nginx + php-fpm.重定向到php-script [英] nginx + php-fpm. Redirect to php-script
问题描述
我的nginx配置部分(成功运行)
My nginx config part (successfully working)
... *config* ...
location ~ \.php$ {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php break;
}
set $nocache "";
include fastcgi_params;
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/folder/$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/folder/;
fastcgi_intercept_errors on;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key $host$request_uri;
# fastcgi_cache folder;
fastcgi_cache_valid 200 1m;
fastcgi_cache_bypass $nocache;
fastcgi_no_cache $nocache;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
fastcgi_send_timeout 900;
fastcgi_read_timeout 900;
}
现在我需要为/my/operation =>/my.php?operation添加重写规则
Now I needed to add rewrite rule for /my/operation => /my.php?operation
location /my/ {
rewrite ^(.*)$ /my.php?$1 break;
}
重写规则有效,但php文件正在下载,未执行.
Rewrite rule is working, but php file is downloading, not executing.
我是Nginx的新手,所以我需要帮助
I'm newbie in Nginx, so I need help
推荐答案
您的问题是,通过放置break
,您是在告诉nginx已经完成,并且您不希望进行任何进一步的处理,因此
Your problem is that by placing break
you are telling nginx that you are done and you don't want any furtur processing to happen, so the location ~ \.php$
isn't proceessed, and thus the file is being downloaded.
通过放置last
来代替,您是在告诉nginx重写并重新开始处理,这一次它与location ~ \.php$
匹配,因此文件正在处理中.
By putting last
instead you are telling nginx to do the rewrite and restart the processing again, this time it matches the location ~ \.php$
and thus the file is being processed.
所以最终的解决方案是
location /my/ {
rewrite ^(.*)$ /my.php?$1 last;
}
尽管我通常倾向于以一种更简单的方式编写它,因为您将要匹配整个内容
Though I usually tend to write it in a simpler way since you're going to match the whole thing
location /my/ {
rewrite ^ /my.php?$1 last;
}
您可以阅读文档以查看所有标志及其标志.意义.
You can read the documentation to see all the flags and their meanings.
这篇关于nginx + php-fpm.重定向到php-script的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!