php 一个规则正则表达式的 nginx 多个位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9584630/
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-26 07:11:38  来源:igfitidea点击:

nginx multiple locations for one rule regex

nginxlocationphp

提问by Valentin Stoianoff

I'm trying to force nginx to process through php-fpm following urls:

我正在尝试强制 nginx 通过 php-fpm 处理以下网址:

any url ending with

任何以

  • .php
  • /sitemap.xml
  • /api/api.js
  • .php
  • /站点地图.xml
  • /api/api.js

I'm trying the following regex, but it does not work:

我正在尝试以下正则表达式,但它不起作用:

location ^~ ^((.*\.php)|/sitemap.xml)+$ {

采纳答案by Dayo

Try ...

尝试 ...

location ~ (\.php|sitemap\.xml|api/api\.js)$ {
    ...
}

回答by MTeck

You shouldn't really be trying to force Nginx to send things to PHP. That's kind of 'the wrong way'. You should let Nginx see if the file exists and pass if it doesn't. It's understood that *.php needs to be processed by PHP so you pass .php.

您不应该真的试图强制 Nginx 将内容发送到 PHP。这是一种“错误的方式”。您应该让 Nginx 查看文件是否存在,如果不存在则通过。据了解,*.php需要经过PHP处理,所以传递.php。

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

location ~ \.php {
    fastcgi_pass unix:/tmp/php-fpm.socket;
}

location = /api/api.js {
    fastcgi_pass unix:/tmp/php-fpm.socket;
}

location = /sitemap.xml {
    fastcgi_pass unix:/tmp/php-fpm.socket;
}

Trying to maintain a regular expression for locations when you don't understand how Nginx location blocks work is going to be extremely painful in the long run.

从长远来看,当您不了解 Nginx 位置块的工作原理时,尝试维护位置的正则表达式将是非常痛苦的。

http://wiki.nginx.org/HttpCoreModule#location

http://wiki.nginx.org/HttpCoreModule#location