hopeless-dream

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Nginx中$http_host、$host、$proxy_host的区别

变量 是否显示端口 值是否存在
host

"Host:value"显示

值为a:b的时候,只显示a

http_host "Host:value",value存在就显示
proxy_host

默认80不显示

其他端口显示

"Host:value"显示

通过Nginx配置演示:

复制代码
[root@ans3 conf]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
#     upstream backend {
#       server 10.0.0.50:8080;
#}

    server {
          listen       80;
          server_name  a.test.com;

    location / {
        proxy_pass http://10.0.0.50:8080;
        proxy_set_header X-Proxy-Host $proxy_host;
        proxy_set_header Host $http_host;
           index index.html index.htm;
       }
}
}
复制代码

另一台服务器配置

复制代码
[root@master conf]# cat nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8080;
        server_name  www.test.com aa.test.com;


        location / {
      return 200 'http_host=[$http_host] host=[$host] proxy_host=[$http_x_proxy_host]\n';
}
}
}
复制代码

不携带请求头 Host

[root@ans3 conf]# curl -H 'Host:' --http1.0 http://a.test.com
http_host=[] host=[www.test.com] proxy_host=[10.0.0.50:8080]
 
变量说明
http_host   请求无 Host, 则 http_host 为空, 继而无 Host 传到 proxy
host www.test.com proxy 无 Host 传入, 则使用其 server_name 的第一项
proxy_host
10.0.0.50:8080
取自于 proxy_pass 的参数

携带请求头 Host

[root@ans3 conf]# curl -H 'Host:abc:123' --http1.0 http://a.test.com
http_host=[abc:123] host=[abc] proxy_host=[10.0.0.50:8080]

 

变量说明
http_host abc:123 给啥拿啥
host abc 第一个:前的内容(小写)
proxy_host
10.0.0.50:8080
带端口显示

修改真实服务器的端口为默认端口

复制代码
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
#     upstream backend {
#       server 10.0.0.50:8080;
#}

    server {
          listen       80;
          server_name  a.test.com;
        
    location / {
        proxy_pass http://10.0.0.50:80;
        proxy_set_header X-Proxy-Host $proxy_host;
        proxy_set_header Host $http_host;
           index index.html index.htm;
       }
}
}
复制代码

 

复制代码
http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name www.test.com aa.test.com;


        location / {
      return 200 'http_host=[$http_host] host=[$host] proxy_host=[$http_x_proxy_host]\n';
}
}
}
复制代码

访问时proxy_host会省略80端口

[root@ans3 conf]# curl -H 'Host:abc:123' --http1.0 http://a.test.com
http_host=[abc:123] host=[abc] proxy_host=[10.0.0.50]

 

posted on   hopeless-dream  阅读(31795)  评论(6编辑  收藏  举报

编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示