0

我有一堆需要 .php?id=123 的 PHP 文件,我需要全部获取它们。我如何在我的配置文件中完成它们?

我似乎无法弄清楚如何利用

get1.php?id=stuff
get2.php?id=stuff
get3.php?id=stuff

等等...

问题是当它们都在同一个根目录下时我该怎么做?


使用以下内容我在 p.php?id=945 上得到 500 ERROR 但 PHP 工作正常但我无法登录或获取 POST 数据工作

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;
      }
    }

这:只有 500 秒重写或内部重定向周期,而内部重定向到“/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;
    }
}
4

1 回答 1

0

“这个对吗?”

没有。

1)根不应该在一个位置块中,它应该只在一个服务器块中。

2) Apache rewrite 测试是否是现有文件是使用 try_files 完成的,而不是使用 Nginx rewrite。也可以看看

3)您的 proxy_pass 将传递到必须是循环重定向的同一台服务器。

4) 你在一个位置块内还有一个位置块,这看起来很奇怪。尽管某些高级配置可能需要它,但您正在做的事情并不需要它。

我想你可能希望你的 nginx conf 是这样的:

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;
    }
}

如果您确实希望包含 .php 的唯一请求转到您的p.php文件,那么您应该将最后一个位置块替换为:

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;
}

编辑

我是否需要为每个具有 $_GET['']; 的 php 文件创建特定的重写?

不 - 您应该能够使用 try_files 将它们传递给任何文件,例如:

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;
    }

如果 .php 文件存在并附加了查询字符串,这将匹配对 php 文件的任何 .php 请求,然后如果 .php 文件不存在并传入查询字符串和路径,则返回到 index.php 。

于 2013-04-24T18:03:52.290 回答