npm+vue打包静态文件+端口转发

先说要点,再show code

1,nginx转发不要填写127.0.0.1,localhost等ip地址

2,location根路径要加try_file选项,请求转发到index.html

3,如果有path有/,那就都带上/

 

我的nginx.conf

#user  root;
worker_processes  1;

events {
    worker_connections  1024;
}


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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8081;
        server_name  localhost;
        
        root   C:\Users\Administrator\Documents\s-ui\dist;
        index  index.html index.htm;
         
        location /prod-api/ {
             proxy_http_version 1.1;    
                   proxy_set_header Connection "";
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $remote_addr;
             proxy_pass http://192.168.8.100:8080/;
        }

        location / {
            add_header 'Access-Control-Allow-Origin' '*';
            try_files $uri $uri/ /index.html;  #匹配项目的入口页,前面必须加/
        }

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

    }

}

 

posted @ 2024-03-07 17:04  MoreJewels  阅读(42)  评论(0编辑  收藏  举报