大网数据广告活动
服务器运维问答

Proxy pass through 2 nginx servers

· 52CE 我爱测

I have some issues with nginx and servers. Here's my first configuration: server { listen 80; server_name *.example.com; # Add Headers for odoo proxy mode proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-For …

问题原文

原帖链接 · Loïc Faure-Lacroix · CC BY-SA 4.0

I have some issues with nginx and servers.

Here's my first configuration:

server {
    listen   80;
    server_name  *.example.com;

    # Add Headers for odoo proxy mode
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;

    location / {
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8000/;
    }
}

It then proxy pass to an other nginx server listening on the port 8000 everything matched by the wildcard *.example.com.

Then the next server has a few configurations that are automatically generated. Those looks like this:

upstream upstream_project1 {                                                                                                      
    server 172.18.0.5:8069;
}

server {
    listen   80;
    server_name  project1.example.com;

    location / {
        proxy_redirect off;
        proxy_pass http://upstream_project_1;
    }
}

and

upstream upstream_project2 {                                                                                                      
    server 172.18.0.6:8069;
}

server {
    listen   80;
    server_name  project2.example.com;

    location / {
        proxy_redirect off;
        proxy_pass http://upstream_project_2;
    }
}

Then the request is proxy passed to the application server running according to the upstream defined in each config.

The problem I have is if I have a few projects and each project have different IPs / hostname. The request seems to only go through the first configuration.

It feels like the first proxy send the request and set the headers properly has my application server react as it's correctly proxied.

The problem is that it proxy the wrong application server.

Also, if I kill the application server, that it matches first it will used a different one as if it was falling back to a "default" server.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

nginx uses the Host header to select an appropriate server block, otherwise the default server block (or the first server block if no default is defined) will be used to process the request. See this document for details.

By default, proxy_pass sets the Host header to the value specified in the proxy_pass directive (i.e. 127.0.0.1:8000).

Try adding the following to your reverse proxy configuration:

proxy_set_header Host $host;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-09-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。