1. 程式人生 > 實用技巧 >nginx配置域名代理後訪問域名又自動切換回IP

nginx配置域名代理後訪問域名又自動切換回IP

nginx初始配置為

location ^~ /lucas/ {
  proxy_pass http://xxxx:8080/lucas/;
 }

把配置加多幾行引數,解決訪問域名又自動切換回IP

location ^~ /lucas/ {
  proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   Host      $http_host;
        proxy_set_header X
-NginX-Proxy true; proxy_redirect off; proxy_pass http://xxxx:8080/lucas/; }

Windows本地測試:

1、C:\Windows\System32\drivers\etc 目錄下在hosts檔案裡面加多一行,在本機可以通過域名訪問本機。

127.0.0.1       xxxx.com

2、下載nginx:http://nginx.org/

3、解壓nginx

4、修改conf\nginx.conf檔案,修改server_name為域名,新增多一個代理

server {
        listen       
80; server_name xxxx.com; location ^~ /lucas/ { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_redirect off; proxy_pass http:
//xxxx:8080/lucas/; } 。。。。。。 }

5、nginx.exe目錄下開啟cmd,執行命令開啟nginx

6、在本機瀏覽器訪問:http://xxxx.com/lucas/

start nginx : 啟動nginx
nginx -s reload :修改配置後重新載入生效
nginx -s reopen :重新開啟日誌檔案
nginx -t -c /path/to/nginx.conf 測試nginx配置檔案是否正確

nginx -t                         # 檢視nginx狀態

關閉nginx:
nginx -s stop :快速停止nginx
nginx -s quit :完整有序的停止nginx

更多nginx知識可參考:https://www.cnblogs.com/loong-hon/p/9060515.html