今天在公司里遇到一個問題,就是一個服務器上發布兩個tomcat,端口號什么的都改完了,就是證書這一塊過不去。突然想起來nginx可以做反向代理,就試着用了一下nginx。
以前也沒有用過,於是在網上查找了大量的關於nginx的文章。在這里不得不吐槽一下, 網上的坑實在是太多了,不知道是我的理解有問題,還是濫竽充數的人太多。試了好多次終於讓我搞定了。話不多說,直接貼配置,參數詳情就不在這里多做解釋。
注:由於用的是windows服務器,要修改服務器的host文件 在最后一行加上 127.0.0.1 test.myweb.com 127.0.0.1 test.myweb1.com 。一定要換行!!! linux的沒試過,不知道要不要修改系統文件。
1 http { 2 include mime.types; 3 default_type application/octet-stream; 4 5 sendfile on; 6 //這里的test.myweb.com要和你的server_name對應 7 upstream test.myweb.com{ 8 //這是你的tomcat地址加端口 9 server localhost:8080; 10 } 11 //同上 12 upstream test.myweb1.com{ 13 server localhost:8088; 14 } 15 //第一個代理 16 server { 17 //你的nginx端口默認80 18 listen 80; 19 //和上面的upstream對應 20 server_name test.myweb.com; 21 //攔截的你訪問請求 22 location / { 23 攔截的請求地址,和你的server_name對應 24 proxy_pass http://test.myweb.com; 25 proxy_set_header Host $http_host; 26 proxy_set_header X-Real-IP $remote_addr; 27 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 28 } 29 } 30 //第二個代理,同上,和第二個upstream對應 31 server { 32 listen 80; 33 server_name test.myweb1.com; 34 35 location / { 36 proxy_pass http://test.myweb1.com; 37 proxy_set_header Host $http_host; 38 proxy_set_header X-Real-IP $remote_addr; 39 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 40 } 41 } 42 43 44 }