Nginx重写了通配符域

时间:2014-04-25 19:49:58

标签: nginx url-rewriting rewrite subdomain wildcard

我设置了通配符域,我正在努力使重写规则适用于一个子域而不是另一个子域,也不适用于主域。

server{
    listen 80 default_server;

    server_name    _;
    root           /usr/share/nginx/html/$http_host;
    index          index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    include /usr/share/nginx/conf/mission13.io.conf;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

正如你所看到的那样,我将mission13.io.conf包含在我的重写内容中,我尝试了一些不同的东西,但没有任何效果,例如:

rewrite ^http://sub.mission13.io/(.+)/(.+)$ /index.php?page=$1&action=$2&ajax=0;

我也试过这个:

location sub.missison13.io/ {
    rewrite ^(.+)/(.+)$ /index.php?page=$1&action=$2&ajax=0;
}

这两种方法都不起作用。我该怎么办呢?

1 个答案:

答案 0 :(得分:1)

你应该创建一个新的server块并将你的重写放入其中:

server {
    listen 80;

    server_name sub.mission13.io;

    # everything else #

    include /usr/share/nginx/conf/mission13.io.conf;
}

server {
    listen 80 default_server;

    server_name    _;
    root           /usr/share/nginx/html/$http_host;
    index          index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

你可以在没有使用server块的第二个if块的情况下进行此操作,但这样做会很糟糕,不优雅且无效。

if ($http_host = sub.mission13.io) {
    rewrite ^(.+)/(.+)$ /index.php?page=$1&action=$2&ajax=0;
}