phpnginxreverse-proxy

nginx reverse proxy set header Host got 404 error


The following is my reverse proxy configuration. But when I added proxy_set_header Host $host;, there was a 404 error. I don't know where is the problem.

user  www www;
worker_processes auto;
error_log  /home/wwwlogs/nginx_error.log  crit;
pid        /usr/local/nginx/logs/nginx.pid;
google_perftools_profiles /tmp/tcmalloc;
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept on;
    }
http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile   on;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        server_tokens off;
        access_log off;

server
{
    listen 80 default_server;
    server_name _;
    index index.html index.htm index.php;
    root  /home/wwwroot/default;
    include enable-php.conf;
    location /nginx_status
    {
        stub_status on;
        access_log   off;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }
    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }
    location ~ /.well-known {
        allow all;
    }
    location ~ /\.
    {
        deny all;
    }
    access_log  /home/wwwlogs/access.log;
}
include vhost/*.conf;

server
{
    listen 80;
    include /usr/local/nginx/conf/domains.txt;
    location /
    {
        proxy_set_header Host $host;# Got 404 with this
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://example.com/;
    }
    access_log  /home/wwwlogs/www_access.log;
}

}

the content of /usr/local/nginx/conf/domains.txt:

server_name example_A.com;

I run curl -I http://example_A.com, got this res:

HTTP/1.1 200 OK
Server: nginx
Date: Sun, 02 Dec 2018 03:19:24 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Powered-By: PHP/7.2.6
Set-Cookie: PHPSESSID=qurm52c7jltd0quhvqud58nd2d; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache

There is no error log in the log folder.

I have search for solution for hours, hope somebody can help me.

There are two domain on host (example.com),when i add proxy_set_header Host $host, it return the wrong web page.

Finally, i find a way that work on my situation, still don't know the reason. I use custom variable X-Real-HOST instead of Host, and use $_SERVER['HTTP_X_REAL_HOST'] to get the host.


Solution

  • A proxy_pass does not change the host to the host you are attacking.

    If you arrive to this server with foo.com and you proxy pass to example.com , the server in example.com will recive the connection as Host="foo.com", not as "example.com".

    The 404 you are getting, is not from this configuration, but from the example.com configuration. Look there if there is an accepting configuration for server_name $host. The error comes from here, but from "example.com".