【问题标题】:Directory Override with NGINX使用 NGINX 覆盖目录
【发布时间】:2018-01-19 04:37:18
【问题描述】:

我需要在我的 Web 服务器上覆盖 NGINX 规则的特殊目录,以便适用于 /var/www/acme 的有关 CSS、JS 等的规则不适用于 /var/www/acme/special。我有一个 /var/www 包含我在 me.com 下的网站(127.0.0.1 localhost 设置)。在里面,我有一个 acme 文件夹,它为我的 PHP 使用了一个特殊的框架。但是,我有一个文件夹 /var/www/acme/special 不需要应用这些相同的规则,只需像普通的 PHP 网站一样运行。我被卡住了,因为当我为 /var/www/acme/special 应用具有特殊位置和 try_files 例程的逻辑以及中断时,我收到一个 HTTP 500 错误,上面写着“内部时重写或内部重定向周期重定向到“/acme/special///////////”。

这是我在 Ubuntu 16.04 上用于 NGINX 的 /etc/nginx/sites-available/me.com 文件。我做错了什么?

server {

    listen 80;
    listen [::]:80;

    root /var/www;

    index index.php index.html;

    server_name me.com www.me.com;


    location /acme/special {
        try_files $uri $uri/;
        break;
    }

    location ~ ^/acme/(.*(?<!boot))/css/(.*)$ {
        alias /var/www/acme/$1/views/assets/css/$2;
    }

    location ~ ^/acme/(.*(?<!boot))/js/(.*)$ {
        alias /var/www/acme/$1/views/assets/js/$2;
    }

    location ~ ^/acme/(.*(?<!boot))/img/(.*)$ {
        alias /var/www/acme/$1/views/assets/img/$2;
    }

    location ~ ^/acme/(.*)/fonts/(.*(?<!boot))$ {
        alias /var/www/acme/$1/views/assets/fonts/$2;
    }

    location ~ ^/acme/(.*)/boot/(.*)$ {
        alias /var/www/acme/$1/views/assets/boot/$2;
    }

    location / {
        try_files $uri $uri/ @php;
    }

    location @php {
        rewrite ^/acme/([^\/]+)/.*((?!index\.php).)*$ /acme/$1/index.php?query_string last;
    }

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


}

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

    试试:

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

    try_files 语句的最后一个参数是默认操作,因此$uri/ 术语不会被视为文件 术语之一。使用=404/index.php 之类的东西,具体取决于您想要的文件未找到响应。请参阅this document 了解更多信息。

    break 是重写模块的一部分,在这里没有任何用途。

    【讨论】:

      猜你喜欢
      • 2023-03-07
      • 2014-01-09
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 2020-01-12
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多