nginx的GET .PHP变量 [英] nginx GET .php variables
问题描述
我有一大堆的PHP文件采取的.php?ID = 123,我需要让他们所有。我该怎么做他们都在我的配置文件?
我似乎无法弄清楚如何使用
get1.php?ID =东西
get2.php?ID =东西
get3.php?ID =东西
等等...
现在的问题是我怎么做到这一点的时候,他们都是一样的根目录下?
用下面的我得到500错误的p.php?ID = 945,但PHP工作正常,但我不能登录或获取POST数据到工作
服务器{
听80;
服务器名site.com www.site.com;
根/家/网站/的public_html;
位置 / {
指数的index.php index.html的index.htm的;
位置~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
到期1D;
?try_files $ URI $ ARGS @backend;
}
error_page 405 = @backend;
add_header的X缓存从后端HIT;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php文件;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params;
}
位置@backend {
内部;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php文件;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params;
}
位置〜* \(PHP | JSP | CGI | PL | PY)。$ {?
?try_files $ URI $ ARGS的index.php;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php文件;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params;
}
位置〜/\.ht {
拒绝所有;
}
}
本:刚500S 重写或内部重定向周期,同时在内部重定向到的index.php
服务器{
听80;
服务器名site.com www.site.com;
根/家/网站/的public_html;
#try并直接服务于静态文件
位置〜* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $ URI @inPlaceDynamicFile;
期满24小时;
add_header语用公开的;
add_header缓存控制公,必重新验证,代理重新验证;
}
#allow我们有动态的CSS / JS文件
位置@inPlaceDynamicFile {
#例子/css/cssIncludes.css => /css/cssIncludes.css.php
try_files $ uri.php = 404;
fastcgi_pass 127.0.0.1:9001;
包括fastcgi_params.conf;
}
位置 / {
?try_files $ URI $ ARGS /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params.conf;
}
}
这是正确的吗?
都能跟得上。
1)根绝不应该在一个位置块,只应在一台服务器块。
2)的Apache重写进行测试,如果事情是现有文件与try_files不Nginx的重写完成。 <一href="http://stackoverflow.com/questions/16088663/apache-mod-rewrite-to-nginx-rewrite-rules/16095173#16095173">See也
3)您proxy_pass是要通过对已得是一个圆形的重定向相同的服务器传递。
4)你还得到了一个位置块,这似乎很奇怪里面的位置块。虽然这可能需要一些高级配置,你不需要它,你在做什么。
我想你可能希望你的nginx的conf是这样的:
服务器{
听80;
服务器名site.com www.site.com;
根/家/网站/的public_html;
#try并直接服务于静态文件
位置〜* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $ URI @inPlaceDynamicFile;
期满24小时;
add_header语用公开的;
add_header缓存控制公,必重新验证,代理重新验证;
}
#allow我们有动态的CSS / JS文件
位置@inPlaceDynamicFile {
#例子/css/cssIncludes.css =&GT; /css/cssIncludes.css.php
try_files $ uri.php = 404;
fastcgi_pass 127.0.0.1:9000;
包括fastcgi_params.conf;
}
位置 / {
try_files $ URI /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params.conf;
}
}
如果你想包含的.php唯一的请求转到您的 p.php
文件,那么你应该替换的最后一个单元块:
位置〜/(.*)\.php {
try_files $ URI /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params.conf;
}
位置 / {
try_files的index.php = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params.conf;
}
修改的
我需要创建具有每一个PHP文件的特定重写 $ _GET [''];?
没有 - 你应该能够在通过他们与像try_files的任何文件:
位置/ {
?try_files $ URI $ ARGS /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $ DOCUMENT_ROOT $ fastcgi_script_name;
包括fastcgi_params.conf;
}
如果用追加查询字符串存在,这将匹配任何的.php请求到PHP文件,然后回落到的index.php如果PHP文件不与两个传入的查询字符串和路径存在。
I have a bunch of PHP files that take .php?id=123 and I need to get them all. How do I do them all in my config file?
I can't seem to figure out how to make use of
get1.php?id=stuff
get2.php?id=stuff
get3.php?id=stuff
and so on...
The problem is how do I do that when they are all under the same root directory?
With the following I get 500 ERROR on the p.php?id=945 but PHP works fine but I CANT login or get POST data to work
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
location / {
index index.php index.html index.htm;
location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
expires 1d;
try_files $uri?$args @backend;
}
error_page 405 = @backend;
add_header X-Cache "HIT from Backend";
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location @backend {
internal;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(php|jsp|cgi|pl|py)?$ {
try_files $uri?$args /index.php;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
THIS: just 500s rewrite or internal redirection cycle while internally redirecting to "/index.php"
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri @inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location @inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9001;
include fastcgi_params.conf;
}
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
"Is this correct?"
Nope.
1) Root should never be in a location block, it should only be in a server block.
2) Apache rewrite for testing if something is an existing file is done with try_files not with Nginx rewrite. See also
3) Your proxy_pass is going to be passing through to the same server which has got to be a circular redirect.
4) You've also got a location block inside a location block, which seems quite odd. Although that may be needed for some advanced config, you don't need it for what you're doing.
I think you probably want you nginx conf to be like this:
server {
listen 80;
server_name site.com www.site.com;
root /home/site/public_html;
#try and serve static files directly
location ~* ^[^\?\&]+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
try_files $uri @inPlaceDynamicFile;
expires 24h;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
#allow us to have dynamic css/js files
location @inPlaceDynamicFile {
# examples /css/cssIncludes.css => /css/cssIncludes.css.php
try_files $uri.php =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params.conf;
}
location / {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
}
If you do want the only requests that contain .php to go to your p.php
file then you should replace the last location block with:
location ~ /(.*)\.php {
try_files $uri /p.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
location / {
try_files /index.php =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
Edit
Do I need to create specific rewrites for EVERY php file that has a $_GET[''];?
No - you should be able to pass them in to any file with the try_files like:
location / {
try_files $uri?$args /index.php?q=$uri&$args;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params.conf;
}
That would match any .php request to a php file if it exists with the query string appended , and then fall back to index.php if the .php file doesn't exist with both the query string and path passed in.
这篇关于nginx的GET .PHP变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!