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]

 


免責聲明!

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



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