【问题标题】:Nginx rewrites for wildcard domainsNginx 重写通配符域
【发布时间】:2014-06-11 16:17:15
【问题描述】:

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

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

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

【问题讨论】:

标签: nginx url-rewriting rewrite subdomain wildcard


【解决方案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;
}

【讨论】:

  • 谢谢,我使用if 方法,我知道它效率不高,但我不需要效率,因为网站不会收到很多请求,因为它是为我们的客户和不向公众开放。
猜你喜欢
  • 2012-08-09
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 2014-12-29
  • 2013-02-22
  • 1970-01-01
  • 2014-06-09
  • 1970-01-01
相关资源
最近更新 更多