1. 程式人生 > >nginx 反向代理與單頁應用配置

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;
        }
    }

}