【问题标题】:nginx rewrite to index.php?q=$uringinx 重写为 index.php?q=$uri
【发布时间】:2018-08-27 04:55:15
【问题描述】:

我必须更新一个 nginx 主机,以便所有对别名的请求都被重写为 /alias_path/index.php?q=$uri。但现在所有资产都不再可用。

这是我目前的配置。而且我越来越近了,但是当我取消对最后一个位置的注释时,资产不再可用。

    location /csv-import {
            alias /var/www/csv-import/public;

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

            try_files $uri $uri/ =404;

            #location ~ ^/csv-import/(.*)$ {
                    #alias /var/www/csv-import/public;
                    #try_files $uri $uri/ /csv-import/index.php?q=$1;
            #}

            error_log /var/log/nginx/csv-import.error.log;
            access_log /var/log/nginx/csv-import.access.log;
    }

我不想访问的文件是/var/www/csv-import/public/index.php。所有像 example.com/csv-import/some/url 这样的 Url 都应该重写为 example.com/csv-import/index.php?q=some/url 但像 example.com/csv-import/css/app.css 这样的资产应该在 /var/www/csv-import/public/css/app.css 下可用。

我确信有一个完美的解决方案,但我想不出。

【问题讨论】:

    标签: php nginx nginx-location


    【解决方案1】:

    您不需要另一个 location 块。通常的方法是更改​​try_files 语句的默认操作。但是因为this issue,if 块可能更简单:

    location ^~ /csv-import {
        alias /var/www/csv-import/public;
    
        if (!-e $request_filename) {
            rewrite ^/csv-import/(.*)$ /csv-import/index.php?q=$1 last;
        }
    
        location ~ \.php$ {
            ...
        }
    
        error_log /var/log/nginx/csv-import.error.log;
        access_log /var/log/nginx/csv-import.access.log;
    }
    

    if 块替换了try_files 语句。 ^~ 运算符避免了与其他 location 块的任何歧义。关于if的使用见this caution。

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 2021-05-08
      • 2013-03-13
      • 1970-01-01
      • 2019-05-26
      • 2017-04-07
      • 1970-01-01
      • 2021-06-29
      相关资源
      最近更新 更多