【问题标题】:Nginx with Angular and suburl for Symfony带有 Angular 的 Nginx 和 Symfony 的 suburl
【发布时间】:2022-01-10 23:37:50
【问题描述】:

我正在尝试进行 Nginx 配置,我可以在同一域上同时拥有 Angular 和 Symfony,但在 suburl 上拥有 Symfony。

问题是我一直收到 Symfony 配置的 404 Not Found。

我的网站有一个默认的登录重定向,我被重定向到 url,但是在我得到 404 之后。(test.com/api/connect/azure) 是我被重定向到的 URL .

2022/01/10 15:16:15 [错误] 23#23: *3 open() “/var/www/angular/back/public/connect/azure”失败(2:没有这样的文件 或目录)

server {
        listen 443 ssl;
    
        ssl_certificate /etc/nginx/ssl/test_com.pem;
        ssl_certificate_key /etc/nginx/ssl/test_com.pem.com.key;
    
        server_name test.com;
    
        root /var/www/angular/front/dist;
    
        location /api {
            alias /var/www/angular/back/public;
            index index.php
            try_files $uri /index.php$is_args$args;
    
            location ~ \.php$ {
                fastcgi_pass php8:9000;
                fastcgi_split_path_info ^(.+\.php)(/.*)$;
                include fastcgi_params;
                fastcgi_intercept_errors on;
                # When you are using symlinks to link the document root to the
                # current version of your application, you should pass the real
                # application path instead of the path to the symlink to PHP
                # FPM.
                # Otherwise, PHP's OPcache may not properly detect changes to
                # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
                # for more information).
                fastcgi_param SCRIPT_FILENAME $request_filename;
                # Prevents URIs that include the front controller. This will 404:
                # http://domain.tld/index.php/some-path
                # Remove the internal directive to allow URIs like this
                internal;
            }
        }
    
        location / {
            try_files $uri $uri/ /index.html;
        }
    
        error_log /var/log/nginx/project_error.log;
        access_log /var/log/nginx/project_access.log;
    }

【问题讨论】:

  • 您的主要错误是 try_files 最后一个参数被视为新的 URI,所以它应该是 try_files $uri /api/index.php$is_args$args;。此外,还有一个长期存在的bugaliastry_files 指令一起使用。您确定在 /api/ 前缀下应该有任何静态非 PHP 文件吗?也许/api/... 的所有请求都应该由index.php 处理?或者有几个 PHP 脚本应该在 /api/ 前缀下可用?
  • 是的,/api/ 的所有请求都应该由 Index.php 处理它们不是其他 PHP 脚本,除了应该调用的 Index.php 我添加了 try_files $uri /api/index.php $is_args$args;但我仍然有同样的问题

标签: php angular symfony nginx configuration


【解决方案1】:

您已经嵌套了您的位置指令。我还看到您正在侦听端口 443,但没有提及 nginx 应该响应哪个域请求。

考虑使用子域来托管 Angular 和 Symfony 应用程序,如下所示 angularapp.mydomain.com symfonyapi.mydomain.com

您应该将 Angular 和 Symfony 的配置分开到单独的 .conf 文件中。

【讨论】:

  • 您好,由于公司问题,我无法拥有子域,这就是我尝试这样做的原因,但如果根本不可能。我将只使用 2 个不同的域。
  • 使用嵌套位置块没有问题。无需使用单独的域。将配置拆分为多个 .conf 文件只会降低配置的可读性。
猜你喜欢
  • 1970-01-01
  • 2016-12-31
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
  • 2020-02-12
  • 2013-03-22
  • 1970-01-01
  • 2017-01-11
相关资源
最近更新 更多