【问题标题】:Nginx rewriting urls with PHPNginx 用 PHP 重写 url
【发布时间】:2021-01-06 20:24:27
【问题描述】:

我正在尝试从 apache 迁移到 nginx,但在 URL 重写方面遇到了一些问题。 这是示例的根结构:

  1. dir-1
  2. dir-2
  3. index.php
  4. do.php
  5. desc.ini
  6. .隐藏
  • dir-1
  1. file-1.php
  2. file-2.php

我正在努力实现这些结果:

 1. http://url/dir-1 -> 404 error
 2. http://url/dir-1/ -> 404 error
 3. http://url/dir-1/file-1.php -> 404 error
 4. http://url/dir-1/file-2.php -> 404 error
 5. http://url/desc.ini -> 404 error
 6. http://url/.hidden -> 404 error
 7. http://url/ -> 404 error
 8. http://url/index.php -> 404 error
 9. http://url/do.php -> 404 error
 10. http://url/<param1>/<param2> -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
 11. http://url/<param1>/<param2>/ -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
 12. http://url/<[a-zA-Z0-9]{10}> -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>
 13. http://url/<[a-zA-Z0-9]{10}>/ -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>

现在我成功完成了第 10 步到第 13 步,但是当我尝试执行其他操作时,这些文件是可下载的。 显然这个结构只是一个例子,我试图达到一般规则。

我发布了一个我正在尝试的 nginx 配置示例。

谢谢!

server {
     listen <port>;
     listen [::]:<port>;
     server_name <url>;
     root <path1>;
     index <path1>;
     error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;

     location @error {
             try_files $uri $uri /error.html;
     }
     location ~ \.ini {
             rewrite /.*\.ini /error.html;
     }
     location ~ \.[a-zA-Z0-9]* {
             rewrite /\.[a-zA-Z0-9]* /error.html;
     }
     location ~ /(<param1>)/(<param2>)[/$] {
             try_files $uri $uri/ /do.php?p1=$1&p2=$2;
     }
     location ~ /([a-zA-Z0-9]*)[/$] {
         try_files $uri $uri /index.php?p3=$1;
     }
     location ~ \.php {
         try_files $uri =404;
         include <path2>/fastcgi_params;
         include <path2>/fastcgi.conf;
         fastcgi_pass unix:<path3>/php7.3-fpm.sock;
         fastcgi_split_path_info ^(.+\.php)(/.+)$;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
     }
}

【问题讨论】:

  • 我是否理解正确,除了http://url/&lt;param1&gt;/&lt;param2&gt;http://url/&lt;[a-zA-Z0-9]{10}&gt; 带有或不带有斜杠之外的所有请求都生成404 错误?
  • 是的,是这样的

标签: php nginx url-rewriting


【解决方案1】:

处理错误:

location @error 块是多余的。尝试替换以下内容:

     error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error;

     location @error {
             try_files $uri $uri /error.html;
     }

与:

    error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;

详细了解error_page 指令here

处理规则 1 到 7:

 1. http://url/dir-1 -> 404 error
 2. http://url/dir-1/ -> 404 error
 3. http://url/dir-1/file-1.php -> 404 error
 4. http://url/dir-1/file-2.php -> 404 error
 5. http://url/desc.ini -> 404 error
 6. http://url/.hidden -> 404 error
 7. http://url/ -> 404 error

使用以下内容:

    location = /dir-1 {
        return 404;
    }
    location = /dir-1/ {
        return 404;
    }
    location = /dir-1/file-1.php {
        return 404;
    }
    location = /dir-1/file-2.php {
        return 404;
    }
    location = /desc.ini {
        return 404;
    }
    location = /.hidden {
        return 404;
    }
    location = / {
        return 404;
    }

处理规则 8 和 9:

 8. http://url/index.php -> 404 error
 9. http://url/do.php -> 404 error

这些规则有点棘手,因为涉及到规则 10 到 13。

使用以下内容:

    location = /index.php {
        if ($is_args = "") {
            return 404;
        }

        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location = /do.php {
        if ($is_args = "") {
            return 404;
        }

        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

处理规则 10 到 13:

 10. http://url/<param1>/<param2> -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
 11. http://url/<param1>/<param2>/ -> rewrite: http://url/do.php?p1=<param1>&p2=<param2>
 12. http://url/<[a-zA-Z0-9]{10}> -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>
 13. http://url/<[a-zA-Z0-9]{10}>/ -> rewrite: http://url/index.php?p3=<[a-zA-Z0-9]>

使用以下内容:

    location ~ /(param1)/(param2)/?$ {
        rewrite ^ /do.php?p1=$1&p2=$2;
    }
    location ~ /([a-zA-Z0-9]{10})/?$ {
        rewrite ^ /index.php?p3=$1;
    }

最终配置:

它应该看起来像这样:

server {
    listen <port>;
    listen [::]:<port>;
    server_name <url>;
    root <path1>;
    index <path1>;

    error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;

    location = /dir-1 {
        return 404;
    }
    location = /dir-1/ {
        return 404;
    }
    location = /dir-1/file-1.php {
        return 404;
    }
    location = /dir-1/file-2.php {
        return 404;
    }
    location = /desc.ini {
        return 404;
    }
    location = /.hidden {
        return 404;
    }
    location = / {
        return 404;
    }

    location = /index.php {
        if ($is_args = "") {
            return 404;
        }

        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location = /do.php {
        if ($is_args = "") {
            return 404;
        }

        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /(param1)/(param2)/?$ {
        rewrite ^ /do.php?p1=$1&p2=$2;
    }
    location ~ /([a-zA-Z0-9]{10})/?$ {
        rewrite ^ /index.php?p3=$1;
    }

    location ~ \.php {
        try_files $uri =404;
        include <path2>/fastcgi_params;
        include <path2>/fastcgi.conf;
        fastcgi_pass unix:<path3>/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

提示:

如果您希望 Nginx 响应除 /index.php/do.php 之外的所有 PHP 请求,请保留以下内容:

    location ~ \.php {
        try_files $uri =404;
        include <path2>/fastcgi_params;
        include <path2>/fastcgi.conf;
        fastcgi_pass unix:<path3>/php7.3-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

如果不是,替换为:

    location ~ \.php {
        return 404;
    }

【讨论】:

    【解决方案2】:

    所以如果你需要的是

    • 使用do.php 脚本处理http://sitename/&lt;param1&gt;/&lt;param2&gt;http://sitename/&lt;param1&gt;/&lt;param2&gt;/ 请求为http://sitename/do.php?p1=&lt;param1&gt;&amp;p2=&lt;param2&gt;
    • 使用index.php 脚​​本处理http://url/&lt;[a-zA-Z0-9]{10}&gt;http://url/&lt;[a-zA-Z0-9]{10}&gt;/ 请求为http://sitename/index.php?p3=&lt;[a-zA-Z0-9]&gt;{10}
    • 在所有其他情况下生成 HTTP 404 错误

    并且不需要提供静态内容,有一种更简单的方法可以做到这一点而无需任何重写:

    server {
        listen <port>;
        listen [::]:<port>;
        server_name <sitename>;
    
        # you don't need neither 'root' nor 'index' directives
    
        # error page definition taken from Tom Nguyen answer
        error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;
    
        # serve 'http://sitename/<param1>/<param2>' requests
        # regexes for <param1> and <param2> should be changed to something meaningful, i.e.
        # (param1_possible_value1|param1_possible_value2|...) or ([a-zA-Z0-9]+) for any value
        location ~ ^/(<param1>)/(<param2>)/?$ {
            # initialize FastCGI parameters with default values first
            include fastcgi_params;
            # overwrite QUERY_STRING FastCGI parameter with a new value
            fastcgi_param QUERY_STRING p1=$1&p2=$2;
            # hardcode executable script path and name
            fastcgi_param SCRIPT_FILENAME <root_path>/do.php;
            # make FastCGI call
            fastcgi_pass unix:<socket_path>/php7.3-fpm.sock;
        }
    
        # serve 'http://sitename/<[a-zA-Z0-9]{10}>' requests
        # similar to the previous location block
        location ~ ^/([a-zA-Z0-9]{10})/?$ {
            include fastcgi_params;
            fastcgi_param QUERY_STRING p3=$1;
            fastcgi_param SCRIPT_FILENAME <root_path>/index.php;
            fastcgi_pass unix:<socket_path>/php7.3-fpm.sock;
        }
    
        # all other requests should lead to HTTP 404 error
        location / {
            return 404;
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 2014-02-18
      • 2013-01-12
      • 2022-08-19
      • 2016-02-07
      • 1970-01-01
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多