【问题标题】:NGINX Two Directories One URL (Codeigniter, HTML)NGINX 两个目录一个 URL (Codeigniter, HTML)
【发布时间】:2021-11-02 17:59:16
【问题描述】:

这是我的 default.conf 文件:

server {
    listen 80;
    listen [::]:80;

    root /var/www/html/;
    server_name example.com;

    # Site 01
    location /* {
        alias /var/www/html/home/;
        index index.htm index.php;
        try_files $uri $uri/ /home/index.html;
    }

    # Site 2
    location /shadows {
        alias /var/www/html/shadows/;
        index index.htm index.php;
        try_files $uri $uri/ /shadows/index.php;
    }

    error_page 404 /error/404.php;
    fastcgi_intercept_errors on;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

我有一个 Codeigniter 应用程序

var/www/html/shadows

我也有一个 HTML 网站

var/www/html/home

问题:

我已经能够对目录 home 或 shadows 进行这项工作,但不能同时用于两者。

如果我将 root 更改为 /var/www/html/home,HTML 站点就会弹出。

目前只有 shadows codeigniter 站点使用此配置弹出。

请告诉我您对解决此问题的想法。非常感谢。

【问题讨论】:

  • “两个目录一个 URL” - 我不清楚你真正想要实现什么......你不能让一个 URL 指向两个位置......?
  • 是的,我希望 www.url.com 指向位于 var/www/html/home 的 html 站点,并且我希望 url.com/shadows 指向 var/www/html/shadows阴影部分是我的 codeigniter 应用程序。
  • 您的 PHP 处理程序 location ~ \.php$ { ... } 使用您的默认根 /var/www/html/,因此无法找到您的 Codeigniter PHP 文件。此外,将location /* { ... } 更改为location / { ... }。您在/var/www/html/home 的 HTML 站点是否也使用 PHP 文件?
  • 其中一些,是的。我在那里有一个无头 CMS。
  • @d7l2k4 已回答。当您在涉及超过两个人的讨论下撰写评论时,为确保您的评论将传送到正确的人员索引,请以用户名开始您的评论,例如@IvanShatsky。我很容易错过你的回复。

标签: nginx nginx-config nginx-location


【解决方案1】:

您可以通过map 块获取root 指令值:

map $uri $approot {
    ~^/shadows/    /var/www/html;
    default        /var/www/html/home;
}
server {
    listen 80;
    listen [::]:80;

    root $approot;
    server_name example.com;

    # Site 01
    location / {
        index index.htm index.php;
        try_files $uri $uri/ /index.html;
    }

    # Site 2
    location /shadows/ {
        index index.htm index.php;
        try_files $uri $uri/ /shadows/index.php$is_args$args;
    }

    error_page 404 /error/404.php;
    fastcgi_intercept_errors on;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

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

我假设您的 HTML 站点应该在根前缀 / 下提供,而不是在 /home/ 下提供,因此我将您的 try_files $uri $uri/ /home/index.html; 指令更改为 try_files $uri $uri/ /index.html;。我还用root 替换了你所有的alias 指令。正如alias 指令documentation 所说,

当位置与指令值的最后一部分匹配时:

location /images/ {
    alias /data/w3/images/;
}

最好改用root 指令:

location /images/ {
    root /data/w3;
}

此外,当您同时使用aliastry_files 指令时,还有一个众所周知的bug,因此使用root 更可靠。

【讨论】:

    【解决方案2】:

    如果我理解正确的话,这样的事情应该可以工作:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        server_name example.com;
    
        root /var/www/html/home;
    
        location /shadows {
            root /var/www/html;
            index index.php;
        }
    }
    

    http://example.com/shadows/index.php 在这种情况下将解析为路径 /var/www/html/shadows/index.php

    【讨论】:

    • 我完全按照上面的说明进行操作。但是,我在 myip/shadows 找不到 NGINX 404,但 myip 有效
    • 如果通过 IP 访问,您必须确保这是您的 default_server... 我刚刚做了一个可以提供帮助的编辑。
    • 仍然 404 未找到错误。请注意,我的主页位于 var/www/html/home 中,而我的影子后端位于 var/www/html/shadows 中,这是否会改变配置?我做了 systemctl restart nginx
    猜你喜欢
    • 2018-02-24
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多