Skip to content

Instantly share code, notes, and snippets.

@zhangskills
Created September 4, 2014 08:18
Show Gist options
  • Save zhangskills/907084648e4d721f5195 to your computer and use it in GitHub Desktop.
Save zhangskills/907084648e4d721f5195 to your computer and use it in GitHub Desktop.
nginx备忘
@zhangskills
Copy link
Author

Nginx反向代理映射成子路径

多个web应用共享一个域名和端口时,可以考虑把不同的web应用映射成不同的子路径,这个子路径在Java EE里称作ContextPath。下面的配置片段解决nginx作为前端,反向代理多个tomcat主机,通过不同子路径共享一个域名的情况。

server {
    listen       80;
    server_name  _;
    index index.html index.htm index.php;
    root /home/dashboard;

    location /dashboard {
        rewrite /dashboard/(.*) /$1 break;
        rewrite ^/dashboard$ /dashboard/ permanent;
        proxy_pass http://127.0.0.1:9082;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

http://codelife.me/blog/2013/09/12/reverse-proxy-for-a-subdirectory-in-nginx/

@zhangskills
Copy link
Author

子目录跳转:

upstream abc{
        server 127.0.0.1:8000;
}

server {
  listen  80;

  server_name abc.com;

gzip on;
gzip_http_version 1.0;
gzip_comp_level 3;
gzip_types text/plain application/x-javascript text/css application/xml;

 location ~ .*\.(?:ico|css|js|gif|jpe?g|png|html)$ {
        proxy_pass http://abc;
        expires 30d;
 }

  location / {
        rewrite /(.*)$ /player/$1 break;
        proxy_pass http://abc;
  }

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment