php CodeIgniter 的 Nginx 重写规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19347306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:20:31  来源:igfitidea点击:

Nginx rewrite rule for CodeIgniter

phpapache.htaccesscodeigniternginx

提问by Letomkov

Here is the rule in English:

这是英文的规则:

Any HTTP request other than those for index.php, assets folder, files folder and robots.txt is treated as a request for your index.php file.

除了对 index.php、assets 文件夹、files 文件夹和 robots.txt 的请求之外的任何 HTTP 请求都被视为对您的 index.php 文件的请求。

I have an .htaccessfile that works correctly on Apache server:

我有一个.htaccess在 Apache 服务器上正常工作的文件:

RewriteCond  !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/ [L]

Some correct results for this rule:

此规则的一些正确结果:

example.com= example.com/index.php

example.com= example.com/index.php

example.com/index.php/welcome= example.com/welcome

example.com/index.php/welcome= example.com/welcome

example.com/assets/css/main.css!=example.com/index.php/assets/css/main.css

example.com/assets/css/main.css!=example.com/index.php/assets/css/main.css

I tried some tools to convert from htaccess rule to nginx rule but all were incorrect.

我尝试了一些工具将 htaccess 规则转换为 nginx 规则,但都不正确。

Via http://winginx.com/htaccess(missing the exception rules for assets folder...):

通过http://winginx.com/htaccess(缺少资产文件夹的例外规则......):

location / { rewrite ^(.*)$ /index.php/ break; }

Via http://www.anilcetin.com/convert-apache-htaccess-to-nginx/(error in $1 value):

通过http://www.anilcetin.com/convert-apache-htaccess-to-nginx/(价值 1 美元的错误):

if ( !~ "^(index.php|assets|files|robots.txt)"){
    set $rule_0 1$rule_0;
}
if ($rule_0 = "1"){
    rewrite ^/(.*)$ /index.php/ last;
}

How can I fix this? It's really hard to debug the rule.

我怎样才能解决这个问题?调试规则真的很难。

Here is my nginx config so far:

到目前为止,这是我的 nginx 配置:

server {
    listen       80;
    server_name  www.example.com example.com;

    location / {
        try_files $uri $uri/ /index.php?/$request_uri;
    }

    location ~ \.php$ {
        root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
    }

    location ~ /\.ht {
        deny  all;
    }
}

回答by anvoz

You can add this to your config:

您可以将其添加到您的配置中:

location ~* ^/(assets|files|robots\.txt) { }

This will work correctly with your location /rule.

这将适用于您的location /规则。

Your config also need to add root document and default index file.

您的配置还需要添加根文档和默认索引文件。

...
root /ftp/wardrobe;
index index.php index.html index.htm;

location / {
    try_files $uri $uri/ /index.php?/$request_uri;
}

location ~* ^/(assets|files|robots\.txt) { }
...

回答by cryptic ツ

You want to do this instead:

你想这样做:

if ($request_uri !~ ^/(index\.php|assets|files|robots\.txt)) {
    rewrite ^/(.*)$ /index.php/ last;
}

$request_uriis for the original URI request by the client. If you want the URI request AFTER other Nginx rewrite rules have processed it then you would use $uri. However, for what you are trying to do the prior would be the one you would want.

$request_uri用于客户端的原始 URI 请求。如果您想要在其他 Nginx 重写规则处理完之后的 URI 请求,那么您将使用$uri。但是,对于您正在尝试做的事情,先前的将是您想要的。

Also, you need to escape special regular expression characters like .by using a backslash.

此外,您需要.使用反斜杠来转义特殊的正则表达式字符。

回答by cse031sust02

Please try this. It works for me.

请试试这个。这个对我有用。

server {
    server_name domain.tld;

    root /var/www/codeignitor;
    index index.html index.php;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

    location / {
        # Check if a file or directory index file exists, else route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}