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:WindowsSystem32driversetc 目录下在hosts文件里面加多一行,在本机可以通过域名访问本机。

127.0.0.1       xxxx.com

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

3、解压nginx

4、修改conf ginx.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

原文地址:https://www.cnblogs.com/RealWorld/p/13533643.html