天天看点

nginx配置

server {
        listen       8088;
        server_name  localhost;

        location / {
            root   dist;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html; // vue history模式刷新404问题解决方式
        }
      

  

1:
        location /api {
            proxy_pass  http://172.30.1.123:8081;
        }
        location /api {
            proxy_pass  http://172.30.1.123:8081/;
        }
       // 转发接口后边加  /  可以去掉/api
      
如果启动说语法错误,那就手动输入提示错误的代码
      
2:
        location /api {
            proxy_pass  http://172.30.1.123:8081;
            rewrite "^/api/(.*)$" /$1 break;
        }

rewrite "^/api/(.)$" /$1 break,路径重写:
      
1)"^/api/(.)$":匹配路径的正则表达式,用了分组语法就是*(.)**,把/api/以后的所有部分当做1组;
2)/$1:重写的目标路径,这里用$1引用前面正则表达式匹配到的分组(组编号从1开始,也就是api),即/api/后面的所有。这样新的路径就是除去/api/以外的所有,就达到了去除/api前缀的目的;