求助, nginx 多站点配置,无法通过域名访问,使用 ip 访问会跳转到其中一个站点

2017-02-22 11:03:10 +08:00
 yujizmq

在一台服务器设置了 Nginx 多站点,但是访问这些站点的域名均无法访问,错误如下图:

通过 ip 会跳转到其中一个站点,具体表现为:

全部配置开启的情况下所有域名无法访问, ip 访问显示的是siteD 去除 siteD 后跳转 siteB,去除 siteB 后 404 ,去除 siteB 后跳转 siteA ,去除 siteA 后跳转 siteC

检查了半天还是不知道是啥问题,请各位大神帮忙排查下 /w\

以下为具体配置文件:

nginx.conf

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    server_tokens   off;
    underscores_in_headers off;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

default.conf

server {
    listen       80;
    server_name  _;

    location / {
        return 404;
    }
}

siteA.conf

server {
    listen       80;
    server_name  siteA.domain.com;

    access_log  /var/log/nginx/siteA.access.log  main;

    location / {
        root   /usr/share/nginx/html/siteA;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
         #try_files $uri =404;
         #root /usr/share/nginx/html/siteA;
         fastcgi_split_path_info ^(.+.php)(/.+)$;
         fastcgi_pass unix:/dev/shm/php5.6-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_index index.php;
         include fastcgi_params;
    }
}

siteB.conf

server {
    listen       80;
    server_name  siteB.domain.com;

    access_log  /var/log/nginx/siteB.access.log  main;

    location / {
        root   /usr/share/nginx/html/siteB;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
         try_files $uri =404;
         root /usr/share/nginx/html/siteB;
         fastcgi_split_path_info ^(.+.php)(/.+)$;
         fastcgi_pass unix:/dev/shm/php5.6-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_index index.php;
         include fastcgi_params;
    }
}

siteC.conf

server {
    listen       80;
    server_name  siteC.domain.com;

    access_log  /var/log/nginx/siteC.access.log  main;

    location / {
        root   /usr/share/nginx/html/siteC/;
        index  index.php index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
         try_files $uri =404;
         root /usr/share/nginx/html/siteC/;
         fastcgi_split_path_info ^(.+.php)(/.+)$;
         fastcgi_pass unix:/dev/shm/php5.6-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_index index.php;
         include fastcgi_params;
    }
}

siteD.conf

server {
    listen       80;
    server_name  siteD.domain.com;

    access_log  /var/log/nginx/siteD.access.log  main;

    location ~ ^/(baidu|webscan|yx_scan) {
        root /usr/share/nginx/siteD;
    }

    location / {
	   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 User-Agent $http_user_agent;
	   proxy_set_header Accept-Encoding "";
	   proxy_redirect off;
	   proxy_pass http://127.0.0.1:8080/siteD/;
	   rewrite ^/WebGoat/(.*)$ /$1 last;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

Nginx Info (使用 apt 安装, mainline)

nginx version: nginx/1.11.9
built by gcc 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) 
built with OpenSSL 1.0.1f 6 Jan 2014
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
9363 次点击
所在节点    NGINX
23 条回复
ovear
2017-02-22 11:33:01 +08:00
empty response 的话提供 error log 才知道
访问 ip 访问的网站为 server 域中的第一个配置
Showfom
2017-02-22 11:35:06 +08:00
问问机房有没有设置白名单
yujizmq
2017-02-22 11:43:25 +08:00
yujizmq
2017-02-22 11:44:38 +08:00
@Showfom ucloud ,安全组开了 80 ,应该没啥别的限制了吧?
whx20202
2017-02-22 14:18:05 +08:00
codespots
2017-02-22 14:33:34 +08:00
原来你们都是这样配置 Nginx 的,难道我之前的配置方法一直是错的?你都没做 proxy_pass ,只有 SiteD 做了这个,所以访问了 siteD?正常的配置不应该是先配置 N 个非 80 端口的站点,然后再配置 listen 80 servername ,将请求 proxy_pass 到对应的非 80 端口的站点上?
codespots
2017-02-22 14:38:09 +08:00
我没试过这种在一个配置里完成站点和反向代理的这种方式,我一般是把站点的规则和反向代理的规则分开写
ovear
2017-02-22 14:47:42 +08:00
@yujizmq iptables -F 试试
Showfom
2017-02-22 15:07:25 +08:00
@yujizmq 有 需要域名备案 不然有白名单机制
predator
2017-02-22 15:12:34 +08:00
"无法访问"在 log 中显示为什么具体错误?这个需要再明确一点

和我平时习惯的配置最明显的不同是你第一段 catch all 设置 listen 命令后面没有加上 default_server 标记,官方文档的例子:

In catch-all server examples the strange name “_” can be seen:

server {
listen 80 default_server;
server_name _;
return 444;
}

如果你加上了 default_server ,而 ip 地址不在后面任何一个 server 里面,那这一段肯定能够 catch 到并发回 444
akira
2017-02-22 15:17:15 +08:00
10 楼说的对
yujizmq
2017-02-22 15:22:28 +08:00
@ovear 执行了,没有效果
yujizmq
2017-02-22 15:24:17 +08:00
@predator 具体的 error log 在 3 楼, default_server 后来打上了,目前情况为 ip 访问 404 ,其余 url 依旧不行
luojiyin87
2017-02-22 15:25:08 +08:00
没对应日志,不好分析。
yujizmq
2017-02-22 16:31:25 +08:00
@Showfom 域名确实没有备案,但是在另一台机子(也是 ucloud )上也有使用这个域名,可以正常访问
Showfom
2017-02-22 17:18:31 +08:00
@yujizmq 备案后即可解决
yujizmq
2017-02-22 17:44:25 +08:00
@Showfom /w\手头没有备案过的域名了,个人域名也不打算备案,看来只能丢给运维去玩了
yujizmq
2017-02-22 18:18:25 +08:00
@luojiyin87 需要什么日志我都可以提供, error log 在 3 楼已经提供
sundong
2017-02-22 20:01:06 +08:00
留个邮箱说说 nginx 略懂一点
yujizmq
2017-02-22 20:09:46 +08:00
@sundong mikoto.yu51 -> gmail

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/342258

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX