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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM