运行另一个应用当有一个URL带问号等于react nginx

run another application when there is a URL with a question mark and is equal to react nginx

我有 2 个 React 构建应用程序,只有当 URL 与 www.example.com/inventory/?id=.. 重合时,我才想 运行 其中一个。 . 我的配置 nginx

server {
listen 80 default_server;
    listen [::]:80 default_server;
    server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}

server {
listen 443 ssl default_server;

index index.html;

ssl on;
ssl_certificate /etc/ssl/example.com.crt;
ssl_certificate_key /etc/ssl/example.com.key;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    root /var/www/example.com/;

location / {
    root /var/www/example.com/public/admin;     
    try_files $uri $uri/ =404;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fmp.sock;
    }
}

    location /inventory/?id= {
    root /var/www/example.com/public/inventory;     
    try_files $uri $uri/ =404;
}

我收到 404 nginx 错误,我做错了什么?

查询字符串不是用于匹配 locationrewrite 语句的 normalized URI 的一部分。

您的位置块可以匹配所有以 /inventory/ 开头的 URI,但您需要使用 if 块来拒绝任何没有匹配参数的请求。请参阅 this caution 关于 if.

的用法

例如:

location /inventory/ {
    if ($arg_id = "") { return 404; }

    root /var/www/example.com/public;
    try_files $uri $uri/ =404;
}

请注意,文件路径是由 root 指令的值与 URI 连接而成的,因此除非您打算在路径中包含两个 inventory 目录,否则不应也出现在 root 语句中。有关详细信息,请参阅 this document