nginx multiple locations for one rule regex

六眼飞鱼酱① 提交于 2019-12-10 11:25:48

问题


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

any url ending with

  • .php
  • /sitemap.xml
  • /api/api.js

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

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

回答1:


Try ...

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



回答2:


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.

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.

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



来源:https://stackoverflow.com/questions/9584630/nginx-multiple-locations-for-one-rule-regex

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!