简体   繁体   English

Nginx位置以及可选的子位置

[英]Nginx location with optional sublocation

I have a WordPress site with php-fpm and nginx configured. 我有一个配置了php-fpm和nginx的WordPress网站。 Works well. 效果很好。
Now I created an Angular Universal app which I want to display on the certain page. 现在,我创建了一个Angular Universal应用,该应用要显示在特定页面上。 To be more specific: 更加具体:

I want WordPress to work here: mysite.com/myapp 我希望WordPress在这里工作: mysite.com/myapp
I want Angular to work here: mysite.com/myapp/any/path/ 我希望Angular在这里工作: mysite.com/myapp/any/path/

I tried so many different configurations but nothing works as I want it to. 我尝试了许多不同的配置,但没有达到我想要的效果。 For instance: 例如:

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

location ~ ^/myapp/(.+)$ {
    proxy_pass http://127.0.0.1:4000/$1$is_args$args;
    proxy_redirect off;
    proxy_read_timeout 60s;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    root /var/www/mysite/myapp/dist/;
}

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm/tst04.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

So as you can see I'm trying to redirect all requests starting with /myapp/{something} to the http://127.0.0.1:4000 but it does not work. 如您所见,我正在尝试将所有以/myapp/{something}开头的请求重定向到http://127.0.0.1:4000但它不起作用。 By "does not work" I mean that all requests are redirected to the localhost, even /myapp/ - which I don't understand because /myapp/ does not match the regex /myapp/(.+) (note the + sign). “不起作用”是指所有请求都重定向到本地主机,甚至/myapp/ -我也不明白,因为/myapp/与正则表达式/myapp/(.+)不匹配(请注意+号) 。

I looked into the nginx error log and found that when I enter mysite.com/myapp/ it tries to redirect it to the upstream: "http://127.0.0.1:4000/index.php" which is not desired to me. 我查看了nginx错误日志,发现当我输入mysite.com/myapp/它尝试将其重定向到upstream: "http://127.0.0.1:4000/index.php" ,这对我来说是不需要的。 When I enter mysite.com/myapp/anypath/ it redirects it to the upstream: "http://127.0.0.1:4000/anypath/" which is correct. 当我输入mysite.com/myapp/anypath/它将重定向到upstream: "http://127.0.0.1:4000/anypath/" ,这是正确的。

Question: why it doesn't work? 问题:为什么不起作用?

Dang! ang! I guess I had to describe the problem somewhere to find the solution ;) 我想我必须在某个地方描述问题才能找到解决方案;)
So here's what was happening: 所以这是正在发生的事情:

  • I make a request ex mysite.com/myapp 我通过mysite.com/myapp提出了请求
  • Nginx matches it to the / location Nginx将其匹配到/位置
  • It runs try_files to the end and fires /index.php?$args which resolves to /myapp/index.php 它运行try_files到最后并触发/index.php?$args解析为/myapp/index.php
  • Nginx keeps resolving new location and it matches ^/myapp/(.+)$ so it's being redirected to the http://127.0.0.1:4000/index.php Nginx不断解析新位置,并且它匹配^/myapp/(.+)$因此将其重定向到http://127.0.0.1:4000/index.php

Tadam! 塔达姆! We have the explanation. 我们有解释。 Now how did I solve it? 现在我该如何解决呢? Well, the solution is pretty simple. 好吧,解决方案非常简单。 I don't want my PHP files to be mathed against ^/myapp/(.+)$ regex so I modified it like this: 我不希望我的PHP文件针对^/myapp/(.+)$正则表达式进行数学^/myapp/(.+)$因此我将其修改为:

"^/myapp/([^ php]+)$" (note the quotation marks) "^/myapp/([^ php]+)$" (请注意引号)

and everything started to work as expected. 一切开始按预期工作。

Posting this in case future readers will encounter similar problem. 张贴此以防将来的读者遇到类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM