始终捕获带有正则表达式的nGinx server_name(ec2)

时间:2014-09-05 10:15:06

标签: amazon-web-services nginx

让ec2服务器具有不同的子域,我希望指向不同的服务,尝试使用server_name来捕获每个但不能正常工作,总是默认为第一个conf(admin)。在下面的示例中,我希望example.com使用default.conf和admin.example.com来使用admin.conf。

#nginx.conf

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;
    keepalive_timeout  65;

    gzip  on;
    gzip_comp_level 9;
    gzip_proxied any;
    gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript applicat$
    gzip_min_length  1000;
    gzip_disable     "MSIE [1-6]\.";

    underscores_in_headers on;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*.conf;

    index   index.html index.htm;

}


#/sites-enabled/default.conf

server {

    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;

}


#/sites-enabled/admin.conf

server {

    listen 80;

    server_name ^(www\.)?admin(-dev|-sandbox|)$ *.example.com;
    #server_name admin.example.com # This is still caught by all routes

    access_log  /var/log/nginx/admin.access.log;

    location / {
            root /usr/share/admin-frontend;
            index index.html index.html;
    }

}

我已经设置了主机文件

127.0.0.1 localhost
127.0.0.1 admin.localhost
127.0.0.1 admin-dev.localhost
127.0.0.1 admin-sandbox.localhost

目前,如果我将公共DNS用于ec2(http://ec2-XX-XX-XX-XX.compute-1.amazonaws.com/),即使服务器不匹配,也会触发管理员配置。

1 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

server_name ~^(www\.)?admin(-dev|-sandbox|)\.example\.com$;

我无法在您的hosts文件中找到* .example.com域,也许您还需要添加它们

127.0.0.1 example.com admin.example.com admin-dev.example.com