문제

I have wildcard domains set up, and I am trying to make it so that rewrite rules apply to one subdomain and not another subdomain, and also not to the main domain.

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;
    }
}

As you can see I am including mission13.io.conf in there is where my rewrites will be, I have tried a few different things and nothing has worked, such as this:

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

I have also tried this:

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

Neither of those two methods work. What could I do to do this?

도움이 되었습니까?

해결책

You are supposed to create a new server block and put your rewrites in it:

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;
    }
}

Of couse you can do it without a second server block using a if block, but that would be bad, not elegant and inefficent.

if ($http_host = sub.mission13.io) {
    rewrite ^(.+)/(.+)$ /index.php?page=$1&action=$2&ajax=0;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top