无法匹配具有双IP地址的服务器

时间:2013-08-11 10:55:20

标签: nginx

我有一个带有两个ip的服务器:当我使用nginx作为jboss7的反向代理时, 为了防止直接访问使用ip地址,(我们已经配置了dns), 我使用下面的配置:

# You may add here yourdefault_server;
# server {
#
server {
    listen *:80;
    server_name _;
    return 404;
}

server {

    listen  80;
    listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

server_name www.shikuaigou.com localhost;
charset utf-8;

location / {
    proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-For  $http_x_forwarded_for;  
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;   
    proxy_set_header   Host $host;
            proxy_set_header   X-Forwarded-Host $host;
    proxy_set_header   X-Forwarded-Server $host;
    proxy_pass http://jboss;
}

location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
}
}
server {
listen 80;
    server_name example.com;
    rewrite "^/(.*)$" http://www.example.com/$1 permanent;
}

server {

    listen 12.34.56.78;

    server_name www.example.com;

    root /usr/share/nginx/html;

    index index.html index.htm;

    location / {
            proxy_set_header   X-Forwarded-For  $http_x_forwarded_for;
            proxy_set_header   Host $host;
            proxy_set_header   X-Forwarded-Host $host;
            proxy_set_header   X-Forwarded-Server $host;
            proxy_pass http://jboss;
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ /index.html;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }
}

但只有在ip上才能匹配服务器whitch返回404,另一个不能匹配

配置 server_name _;

导致这个?

1 个答案:

答案 0 :(得分:1)

因为您有listen 12.34.56.78;所以nginx会选择此服务器来处理12.34.56.78上的请求,因为它更具体针对该IP。

请注意,server_name _;实际上没有任何意义,除了域名不正确。

参考: