nginx 反向代理与单页应用配置

nginx代理配置完之后,nginx配置proxy_pass,需要注意转发的路径配置.

不带/

1

2

3

4

location /test/

{

                proxy_pass http://t6:8300;

}

 带/

1

2

3

4

location /test/

{

                proxy_pass http://t6:8300/;

 }

上面两种配置,区别只在于proxy_pass转发的路径后是否带 “/”

针对情况1,如果访问url = http://server/test/test.jsp,则被nginx代理后,请求路径会便问http://proxy_pass/test/test.jsp,将test/ 作为根路径,请求test/路径下的资源

针对情况2,如果访问url = http://server/test/test.jsp,则被nginx代理后,请求路径会变为 http://proxy_pass/test.jsp,直接访问server的根资源

单页应用配置nginx

关键是rewrite

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       8081;
        server_name  localhost;
        location /proxy/ {
            proxy_pass  http://192.168.0.207:8080/;
        }
        location / {
            root /usr/share/nginx/html;
            index  index.html index.htm;
            rewrite ^/.*/$ / last; # Redirect everything to / (ex index.html) and let the JS router take care of the rest
            rewrite ^([^.]*[^/])$ $1/ permanent; # Force trailing slash
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}

猜你喜欢

转载自blog.csdn.net/u010537398/article/details/81217408
今日推荐