2016-11-22 113 views
1

經過數小時和數天的嘗試讓NGINX反向代理正常工作後的第一篇文章。NGINX反向代理無法通過Namecheap解析subdomains的server_name

我有經由Namecheap服務多個子域指向家在端口80,與路由器/防火牆上的端口上運行,其指向內部NGINX反向代理服務器的OpenWrt(CentOS的7)80.

的子域這個例子是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 proxy_params文件/ nginx的,以前不在那裏:

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; 

我有2子域裏面/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指令中使用顯式通配符,這似乎沒有幫助。

+0

感謝您的回覆。我認爲現在可用的站點是遺留的,並將虛擬主機直接放入conf.d目錄中?我願意使用當前版本的NGINX推薦的版本。 –

回答

0

經過這麼多的故障排除,我終於修好了!去圖上面的代碼是完全正確的,如果有人想用它作爲模板。

問題是我在namecheap中設置了URL重定向設置,而不是每個子域的A記錄 - >我的外部IP。不能相信它竟然是如此簡單,DNS爲亞...