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

Nginx - Reverse Proxy is trying to access backend over IP instead of domain name

· 52CE 我爱测

I am trying to setup a reverse proxy in nginx to serve static contents. I am having issue in correctly setting up; below is my nginx config: server { listen 80; server_name mysub.domain.com; access_log off; resolver 8.8.8.8 valid…

问题原文

原帖链接 · Alyas · CC BY-SA 4.0

I am trying to setup a reverse proxy in nginx to serve static contents. I am having issue in correctly setting up; below is my nginx config:

server {
    listen       80;
    server_name  mysub.domain.com;
    access_log off;

   resolver 8.8.8.8 valid=5s;
   set $backend "www.originaldomain.com";
   location / {

      proxy_cache my_cache;
      proxy_pass https://$backend;
   }
}

If I try a resource of originaldomain over mysub.domain.com .. it wouldn't work.. and if I see error log, it seems the nginx is requesting the resource to originaldomain's IP like below;

2018/06/09 17:47:13 [crit] 9825#0: *111 mkdir() "/store/cache/1" failed (13: Permission denied) while reading upstream, client: 112.123.121.11, server: mysub.domain.com, request: "GET /path/to/image.png HTTP/1.1", upstream: "https://101.181.11.120:443/path/to/image.png", host: "mysub.domain.com"

and when looking up image.png over IP, it cannot be found, to download resource correctly it needs to be looked up through domain name instead of its IP.

Any ideas?

Thanks in advance.

已采纳解答

原帖链接 · wurtel · CC BY-SA 4.0

nginx can only connect to IP addresses, that's how TCP/IP works.

In the location / stanza you need to add a line

proxy_set_header Host $backend;

to pass that header line to the backend.

I'd also investigate that mkdir() "/store/cache/1" failed (13: Permission denied) error. Either configure another location for the cache files or create /store/cache with suitable permissions for the ID nginx is running as.

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

资料来源与适用范围

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

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

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