1、配置IP域名
C:\Windows\System32\drivers\etc\hosts中加入
127.0.0.1 8081.max.com
127.0.0.1 8082.max.com
2、配置nginx.
- upstream tomcatserver1 {
- server 127.0.0.1:8081;
- }
- upstream tomcatserver2 {
- server 127.0.0.1:8082;
- }
- server {
- listen 80;
- server_name 8081.max.com;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- proxy_pass http://tomcatserver1;
- index index.html index.htm;
- }
- }
- server {
- listen 80;
- server_name 8082.max.com;
- #charset koi8-r;
- #access_log logs/host.access.log main;
- location / {
- proxy_pass http://tomcatserver2;
- index index.html index.htm;
- }
- }
流程:
1)瀏覽器訪問8081.max.com,通過本地host文件域名解析,找到本機的服務器(安裝nginx)
2)nginx反向代理接受客戶機請求,找到server_name為8081.max.com的server節點,根據proxy_pass對應的http路徑,將請求轉發到upstream tomcatserver1上,即端口號為8081的tomcat服務器。
4、效果展示
請求8081.max.com,tomcat1接收返回首頁
請求8082.max.com,tomcat2接收返回首頁
nginx代理地址指向修改
/etc/nginx/conf.d/default.conf
前端項目存放的地址
/usr/share/nginx/html/
常用操作
nginx -s stop 快速關閉Nginx,可能不保存相關信息,並迅速終止web服務。
nginx -s quit 平穩關閉Nginx,保存相關信息,有安排的結束web服務。
nginx -s reload 因改變了Nginx相關配置,需要重新加載配置而重載。
nginx -s reopen 重新打開日志文件。
nginx -c filename 為 Nginx 指定一個配置文件,來代替缺省的。
nginx -t 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。
nginx -v 顯示 nginx 的版本。
nginx -V 顯示 nginx 的版本,編譯器版本和配置參數。