ubuntu服务器nginx部署前端记录

159 阅读1分钟

1.安装nginx

ubuntu安装nginx比较简单,在ubuntu终端运行以下指令:

#Nginx安装
apt-get install nginx
#Nginx 查看状态|停止|运行|重启
service nginx status|stop|start|restart

linux系统用yum命令安装

yum install nginx

以下为nginx的文件目录

/usr/sbin/nginx/  #主程序
/etc/nginx/       #配置文件
/usr/share/nginx/ #静态文件
/var/log/nginx/   #日志文件

查找nginx配置文件可以用whereis命令:

whereis nginx.conf

2.安装好nginx后,修改配置文件

主要是修改http部分,配置如下

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root  /home/ubuntu/webApp/dist/; # 路径改成自己的dist路径
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        location /prod-api/{
                proxy_set_header Host $http_host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header REMOTE-HOST $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://localhost:8080/; #设置监控后端启动的端口
        }


        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root html;
        }

  }
}

注意root路径要为前端构建产物dist文件夹的真实路径

配置完成后,运行命令service nginx启动nginx

  • 注意: 如果修改配置文件nginx.conf提示没有权限,先修改该文件的权限再对其进行修改