鍍金池/ 問(wèn)答/Linux/ nginx通過(guò)url訪問(wèn)本機(jī)不同tomcat

nginx通過(guò)url訪問(wèn)本機(jī)不同tomcat

如何通過(guò)nginx來(lái)訪問(wèn)本機(jī)開(kāi)放的不同端口的tomcat,
比如本機(jī)有六個(gè)tomcat

分別為
tomcat1 8080
tomcat2 8081
tomcat3 8082
tomcat4 8083
tomcat5 8084
tomcat6 8085

然后服務(wù)器新部署nginx服務(wù),讓所有的服務(wù)都能通過(guò)nginx的80來(lái)訪問(wèn)
比如http://192.168.102.100/tomcat1 訪問(wèn)tomcat1下面的項(xiàng)目
http://192.168.102.100/tomcat2 訪問(wèn)tomcat2下面的項(xiàng)目
http://192.168.102.100/tomcat3 訪問(wèn)tomcat3下面的項(xiàng)目
http://192.168.102.100/tomcat4 訪問(wèn)tomcat4下面的項(xiàng)目

回答
編輯回答
清夢(mèng)

//把nginx默認(rèn)的配置文件備份下,改改就可以了。
server {
????????#這是你要代理到的80端口
????????listen????80;
????????#這是訪問(wèn)的域名
????????server_name??http://localhost;

????????root html;

????????access_log??logs/nginx.access.log??main;
????????
????????#然后下面才是你要代理的幾個(gè)tomcat
?
????????#默認(rèn)請(qǐng)求
????????location / {
????????????index index.php index.html index.htm;??
????????}
????????location /tomcat1 {

        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8081;
    }
    location /tomcat2 {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8082;
    }
    location /tomcat3 {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8083;
    }
    #其他配置略
}
2017年10月29日 11:58
編輯回答
生性

為什么我訪問(wèn)的結(jié)果是404;直接訪問(wèn)tomcat是可以的

2017年2月4日 10:07
編輯回答
避風(fēng)港

通過(guò)配置文件反向代理即可,在nginx.conf中增加如下location,并將localhost改為需要的ip。

server {
    location /tomcat1 {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8081;
    }
    location /tomcat2 {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8082;
    }
    location /tomcat3 {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_pass http://localhost:8083;
    }
}
2017年10月2日 04:32