【问题标题】:How can I put multiple paths into the same domain in nginx?如何在 nginx 中将多个路径放入同一个域中?
【发布时间】:2022-02-15 22:14:42
【问题描述】:

我想将domain.com/c 连接到/var/www/a/cdomain.com/b/var/www/b。我编写了 nginx 站点可用文件,如下所示:

server {
    listen 443 ...

    index index.php index.html;
    server_name domain.com;
    root /var/www/a;

    location / {
        try_files $uri $uri/ =404;
    }

    location /b {
        root /var/www/b;
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.0-fpm.sock;
    }
}

但是当我输入domain.com/b 时,我看到了 404。我尝试使用alias 而不是root,但得到了相同的结果。我该怎么办?

【问题讨论】:

  • 能否提供nginx的日志?
  • 404 页面出现。日志中没有错误。

标签: php nginx


【解决方案1】:

这是因为您的location ~ \.php$ { ... } 使用了您的全局root /var/www/a;。这只是许多其他解决方案中的两个可能的解决方案:

  1. 使用嵌套的 PHP 处理程序:

    server {
        listen 443 ...
    
        index index.php index.html;
        server_name domain.com;
        root /var/www/a;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location ^~ /b/ {
            root /var/www;
            try_files $uri $uri/ /b/index.php?$query_string;
            location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php8.0-fpm.sock;
            }
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }
    }
    

    或者,如果您的 /var/www/b 是唯一的 PHP 应用程序,您只需将 root 指令添加到您的 PHP 处理程序:

    server {
        listen 443 ...
    
        index index.php index.html;
        server_name domain.com;
        root /var/www/a;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location /b/ {
            root /var/www;
            try_files $uri $uri/ /b/index.php?$query_string;
        }
    
        location ~ \.php$ {
            root /var/www;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }
    }
    
  2. 使用 map 指令从请求 URI 获取您的 Web 根目录:

    map $uri $root {
        ~^/b/    /var/www;
        default  /war/www/a;
    }
    server {
        listen 443 ...
    
        index index.php index.html;
        server_name domain.com;
        root $root;
    
        location / {
            try_files $uri $uri/ =404;
        }
    
        location /b/ {
            try_files $uri $uri/ /b/index.php?$query_string;
        }
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php8.0-fpm.sock;
        }
    }
    

【讨论】:

  • 感谢您的回答。我试过了,但出现 500 错误,并且日志中有以下消息。 rewrite or internal redirection cycle while internally redirecting to "/b/index.php"我用b.domain.com创建另一台服务器的时候,用的是同样的roottry_files,但是没有出现这个错误...
  • 出现两个复制粘贴错误(location ^~ /b/ { ... } 而不是location /b/ { ...}),请立即尝试。
  • 您的/b/ URI 前缀是否等于/var/www/b 文件夹名称?
  • 两者不同。我想我应该将文件夹名称更改为/var/www/sampleb
  • 你应该在你的问题中提到它。当然,上述配置在这种情况下都不起作用。由于this nginx 长期存在的错误,您不能同时使用try_filesalias 指令。最简单的解决方案是根据您的 URI 前缀重命名 /var/www/b 目录。
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多