根据子域映射请求

根据子域映射请求

我该如何映射子域名而不必重写 URI?

server {
    listen       80;
    server_name  cooking.com user.cooking.com admin.cooking.com;
    root   html/cooking/public;

    location / {
        index  index.html index.htm index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  FEED_ENV  default;
        include        fastcgi_params;
    }
    location ~* \.(ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }
}

因此,基本上,当用户访问时,admin.cooking.com它应该请求/index.php/admin/。我本来想使用if,但是如果不好

答案1

为此,您需要单独的服务器块。

# /etc/nginx/sites-available/php.conf

location @php {
  fastcgi_param FEED_ENV default;
  fastcgi_index index.php;
  fastcgi_pass 127.0.0.1:9000;
}

...以及您的服务器配置:

# /etc/nginx/sites-available/server.conf;

listen 80;
root html/cooking/public;
index index.html index.htm index.php;
include fastcgi_params;

location / {
  location ~* \.(ico|css|js|gif|jpe?g|png)$ {
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
  }
  try_files $uri $uri/ @php;
}

...以及您的最终虚拟主机配置:

# /etc/nginx/sites-available/virtual_hosts.conf

server {
  server_name cooking.com;
  include sites-available/server.conf;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include sites-available/php.conf;
}

server {
  server_name user.cooking.com;
  include sites-available/server.conf;
  fastcgi_param  SCRIPT_FILENAME  $document_root/index.php/user/$args;
  include sites-available/php.conf;
}

server {
  server_name admin.cooking.com;
  include sites-available/server.conf;
  fastcgi_param  SCRIPT_FILENAME  $document_root/index.php/admin/$args;
  include sites-available/php.conf;
}

您只需创建一个符号链接sites-enabled即可virtual_hosts.conf

由于我不知道您的具体环境(后端以及它如何处理传入的 URL),因此我无法为您提供完美的配置。但这应该是一个很好的起点。

相关内容