Nginx反向代理
启动两个tomcat作为演示地址。
http://localhost:8080/
http://localhost:8081/
1、修改nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /tomcat1/ {
proxy_pass http://127.0.0.1:8080/;
}
location /tomcat2 {
proxy_pass http://127.0.0.1:8081/;
}
location / {
root html;
index index.html index.htm;
}
}
2、检查配置文件修改是否正确
nginx -t
3、重载配置文件
nginx -s reload
访问http://localhost/tomcat1/test/
访问http://localhost/tomcat2/test/
4、访问404的情况
这里记录一下在配置中出现的404 Not Found 的情况。
当nginx.conf的server配置如下的情况会出现404,那就是proxy_pass 代理的地址最后面少了一个斜杠/
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /tomcat1/ {
proxy_pass http://127.0.0.1:8080;
}
location /tomcat2 {
proxy_pass http://127.0.0.1:8081;
}
location / {
root html;
index index.html index.htm;
}
}