带有或不带有斜杠的嵌套Nginx位置

时间:2020-02-01 18:31:51

标签: nginx

我正在构建一个由后端Laravel和前端Nodejs组成的应用程序。要加入他们,我想通过https://localhost/admin/?路由在Nginx conf中为Laravel创建位置,带有或不带有斜杠。我设法用斜杠来做到这一点,但并非没有。

这是我的配置:

server {
    listen 443 ssl http2;
    server_name localhost;
    ssl_certificate /certs/localhost.crt;
    ssl_certificate_key /certs/localhost.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ALL:!aNULL:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    location / {
      proxy_pass https://nodejs:3000;
      proxy_ssl_verify off;
    }

    # Backend
    location /admin/ {
      alias /var/www/api/public;
      try_files $uri @admin;
      location ~ \.php$ {
          try_files $uri /index.php =404;
          fastcgi_pass php-upstream;
          fastcgi_index index.php;
          fastcgi_buffers 16 16k;
          fastcgi_buffer_size 32k;
          fastcgi_param SCRIPT_FILENAME $request_filename;
          #fixes timeouts
          fastcgi_read_timeout 600;
          include fastcgi_params;
      }
    }

    location @admin {
        rewrite /admin/(.*)$ /admin/index.php?/$1 last;
    }

   # BrowserSync websocket
    location /browser-sync/socket.io/ {
        proxy_pass http://nodejs:3001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

}

我尝试了以下location ~ /admin/?(.*)$ {,但由于Nginx正在寻找不存在的位置而无法使其运行。我该如何实现?

先谢谢了。

1 个答案:

答案 0 :(得分:1)

无论如何,URI /admin应该导致重定向,所以为什么不简单添加:

location = /admin {
    return 301 /admin/;
}