nginx 部署vue 前后端分离遇到问题

记录:

vue项目 npm run build 打包。

会在项目根目录生成dist目录,此目录就是要部署到服务器的代码。

本人后端是springboot+maven  直接maven打jar包。

在服务器配置好运行环境,代码拷贝到服务器。

下载nginx,我这里用的是1.16.0版本。

下面就直接配置nginx:

找到conf目录下的nginx.conf。

下面内容:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        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       8888;#默认端口是80,如果端口没被占用可以不用修改
        server_name  localhost;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
			root E:/sqwyq/dist;
            try_files $uri $uri/ @router;#需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
            index  index.html index.htm;
        }
        #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
        #因此需要rewrite到index.html中,然后交给路由在处理请求资源
        location @router {
            rewrite ^.*$ /index.html last;
        }
		#gateway 加上这段代理,medical对应的prod.env.js中的BASE_URL
		location /api/ {
			proxy_pass http://127.0.0.1:82/api/;
			#proxy_redirect    off;
			proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
			proxy_set_header  X-Real-IP  $remote_addr;
			proxy_set_header  Host $http_host;
			proxy_headers_hash_max_size 51200;
			proxy_headers_hash_bucket_size 6400;
					client_max_body_size    50m; #表示最大上传50M,需要多大设置多大。
		}
		location /sqwy_wechatq {
			alias E:/sqwy_wechatq/h5;
            index  index.html index.htm;
        }
		location /sqwy_map {
			alias E:/sqwy_map/dist;
            index  index.html index.htm;
        }
		location /publicx/api/ {
			proxy_pass http://127.0.0.1:83/publicx/api/;
			#proxy_redirect    off;
			proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
			proxy_set_header  X-Real-IP  $remote_addr;
			proxy_set_header  Host $http_host;
			proxy_headers_hash_max_size 51200;
			proxy_headers_hash_bucket_size 6400;
					client_max_body_size    50m; #表示最大上传50M,需要多大设置多大。
		}
		
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.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;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

遇到问题点:

1.vue项目中的需要添加

2.nginx配置文件中后端配置的ip要写127.0.0.1  如下图:(写v4的ip会报一个错,我也没有明白为什么,反正写127.0.0.1就没问题)

总结:nginx就是先拦截在找location,也就是nginx的代理的作用。

猜你喜欢

转载自blog.csdn.net/qq_41992943/article/details/106722276