Nginx deploys Vue front-end project

Table of contents

1. Install Nginx

1. Installation completed

2. Start Nginx

3. Question

2. Modify the Nginx configuration file


System environment: Mac Pro—version 10.15.7

Nginx version: 1.19.6

1. Install Nginx

brew install nginx

1. Installation completed

Nginx configuration file directory:/usr/local/etc/nginx

Nginx installation directory:/usr/local/Cellar/nginx

2. Start Nginx

brew services start nginx

3. Question

Possible error 1: nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

Solution: The reason is that the default nginx main configuration file location is different from your current one. Use the terminal to enter the Nginx installation location, and then enter [./nginx -h] to view

 Find the -e filename column, copy the path after default, and then use the command to specify the location of the nginx.conf file:

[./nginx -c /usr/local/etc/nginx/nginx.conf], then enter the command to reload: [./nginx -s reload].

2. Modify the Nginx configuration file

Open the Nginx configuration file:/usr/local/etc/nginx/nginx.conf

There are 3 changes that need to be made. Comments have been added to the configuration file below. Please check the instructions starting with " Note "

  1. The front-end packaged file path behind root;
  2. The mapping path behind the location, which is the pathMapping value configured by Swagger on the server side;
  3. The local IP address behind proxy_pass;
#user  nobody;
worker_processes  1;

#pid        logs/nginx.pid;
pid        /usr/local/Cellar/nginx/1.19.6/logs/nginx.pid;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;

        #access_log  logs/host.access.log  main;

        #注意:修改root后面的地址为你打包好的dist文件夹前端项目路径
        location / {
            root   xx/yy/zz/dist;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }

        #注意:/prod-api/ 为你的服务端Swagger中配置的pathMapping属性值
        #注意:修改proxy_pass后面的地址为你部署机器的ip地址,切记端口号后面斜杠不要去掉【/】
        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://0.0.0.0:8081/;
		}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504  /50x.html;
        location = /50x.html {
        root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        #省略.......
    }

    include servers/*;
}

After configuring, enter the command to reload [./nginx -s reload].

Then enter the local IP address in the browser to access it. Note: If the port number of the server starts with 80, there is no port number after the IP address .

Guess you like

Origin blog.csdn.net/u010263943/article/details/128136131