nginx反向代理配置

在web开发中,nginx一般被用作网关,配置路由功能,在使用nginx进行反向代理的过程中,碰到一些配置问题,特此文记录

由于项目中涉及到phpjavaweb服务,在使用nginx进行转发过程中,需要通过rewritelocation配合反向代理,配置略微繁琐,废话不多说直接上nginx的配置。

Nginx的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
location / {
# Filename Not Found
if (!-e $request_filename) {
rewrite "^/(.*)$" /index.php last;
}
# Uri contains php-web url
if ( $uri ~ /php-web ) {
rewrite ^/$/index.php last;
}
# Uri not contains php-web url
if ( $uri !~ /php-web ) {
rewrite / /java-web last;
}
try_files $uri $uri/ /index.php;
}
# Filter java-web uri
location ^~ /java-web {
proxy_pass http://ip:port/java-web;
}

由于nginx没有if else之类的结构,通过多个if结构进行过滤,上述配置的项目实现了:

  • 如果请求的uri中含有php-web将进行php web处理
  • 如果请求的uri中不含有php-web将进行java web处理

参考链接

分享到