NGINX反向代理不通过Namecheap解析子域的server_name

时间:2016-11-22 17:26:36

标签: redirect nginx proxy reverse server-name

首先在这里发布许多小时和几天后试图让NGINX反向代理工作。

我有多个子域通过Namecheap服务指向端口80上的home,路由器/防火墙正在运行OpenWrt,它指向端口80上的内部NGINX反向代理服务器(CentOS 7)。

此示例的子域名是dokuwiki.namecheaptld.io和observium.namecheaptld.io

这是我的主要/etc/nginx.conf文件:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        #listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        #location / {
        #}

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

我在/ etc / nginx中创建了一个之前不存在的proxy_params文件:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

我在/etc/nginx/conf.d/中有两个名为dokuwiki.conf和observium.conf的子域:

upstream dokuwiki  {
      server 192.168.1.6:8180;
}

server {
    listen       80;
    server_name  dokuwiki.namecheaptld.io;

    access_log  /var/log/nginx/dokuwiki.access.log  main;
    error_log  /var/log/nginx/dokuwiki.error.log;

     location / {
     proxy_pass  http://192.168.1.6:8180;
     include /etc/nginx/proxy_params;
     }
}


upstream observium  {
      server 192.168.1.8:80;
}

server {
   listen      80;
   server_name observium.namecheaptld.io
   access_log  /var/log/nginx/observium.access.log  main;
   error_log   /var/log/nginx/observium.error.log;

   location / {
        proxy_pass  http://192.168.1.8:80;
        include /etc/nginx/proxy_params;
    }
}

每当我尝试访问subdomain.namecheaptld.com时,我都会看到在server_name块中使用eith _或default_server定义的默认虚拟主机。

我完全感到困惑,为什么在尝试了很多事情之后它没有工作,包括添加一个尾随/在proxy_pass定义的IP之后,并查看/var/log/nginx/dokuwiki.access.log中的HTTP头是:

[22/Nov/2016:09:29:54 -0500] "GET / HTTP/1.1" 200 18273 "http://dokuwiki.namecheaptld.io/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"
[22/Nov/2016:09:29:54 -0500] "GET /lib/exe/indexer.php?id=start&1479824990 HTTP/1.1" 200 42 "http://MYEXTERNALIP/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"
[22/Nov/2016:09:29:57 -0500] "GET / HTTP/1.1" 200 18264 "http://observium.namecheaptld.io/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"
[22/Nov/2016:09:29:57 -0500] "GET /lib/exe/indexer.php?id=start&1479824994 HTTP/1.1" 200 42 "http://MYEXTERNALIP/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36" "-"

因此,对于上面的标题,当我在192.168.1.11:80上进行监听时,看起来它正在接受dokuwiki.namecheaptld.io或observium.namecheaptld.io的请求并处理请求,就像它是dokuwiki.namecheaptld一样。 IO

我尝试在server_name指令中使用显式通配符,这似乎没什么帮助。

1 个答案:

答案 0 :(得分:0)

经过这么多故障排除我终于解决了!如果有人想将它用作模板,那么上面的代码是完全正确的。

问题是我在namecheap中设置了URL重定向,而不是每个子域的A记录 - >我的外部IP。不敢相信它变得如此简单,DNS为你...