0

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.

4
  • The service at http://example.com is returning the 404 response. You should probably be looking there. Commented Nov 28, 2018 at 12:34
  • when I remove the proxy_set_header Host $host;, everything got ok Commented Nov 28, 2018 at 12:36
  • If you remove it, the default value becomes proxy_set_header Host "example.com";. Why do you need to set it to $host instead of example.com? Commented Nov 28, 2018 at 12:43
  • I need to know the real domain. Commented Nov 30, 2018 at 0:41

1 Answer 1

2

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".

Sign up to request clarification or add additional context in comments.

3 Comments

I will check that. If i remove proxy_set_header Host $host, the host i got is example.com
That's true if you remove the Host $host. If you put it, you try a connection to the example.com server, with the host you have entered at this server, if you don't put any, the defaut is the host you are doing proxy_pass. The problem with the $host, is that it's not configured in the destiny server, and example.com it is. The problem is not in this server, but in the "example.com" server
I have two site content on the host , nginx render wrong directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.