Nginx 配置用于将通配符 server_name 与特定的 Symfony 路由进行匹配

Nginx 配置用于将通配符 server_name 与特定的 Symfony 路由进行匹配

我在 domain.tld 上有一个 Symfony 5 应用程序,它具有以下基本的 nginx 配置:

map "$http_cloudfront_forwarded_proto:$http_x_forwarded_proto" $cloudfront_proto {
    default "http";
    "~https" "https";
}

server {
    server_name domain.tld;
    listen 80 backlog=65535;

    root /var/www/project/public;

    client_max_body_size 500M;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    # optionally disable falling back to PHP script for the asset directories;
    # nginx will return a 404 error when files are not found instead of passing the
    # request to Symfony (improves performance but Symfony's 404 page is not displayed)
    # location /bundles {
    #     try_files $uri =404;
    # }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        # optionally set the value of the environment variables used in the application
        # fastcgi_param APP_ENV prod;
        # fastcgi_param APP_SECRET <app-secret-id>;
        # fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";
        fastcgi_param HTTP_X_FORWARDED_PROTO $cloudfront_proto;

        # 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 $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # 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;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;
}

我的用例:

我有几条路线:

/site/{siteUrl}
/site/{siteUrl}/*
/site/{siteUrl}/*/*
...

喜欢 :

我想使用类似这样的 URL 访问此路线

我不想要重定向,但是所有以 prefix.domain.tld 开头的 URL 的重写规则都指向 /site/prefix.domain.tld URL,并且所有其他静态资产 URL(如 prefix.domain.tld/build/css/custom.css)都有效。

我尝试使用以下命令添加一个新的服务器块:

server {
    server_name prefix.*;
    root /var/www/project/public;
    listen 80;

    client_max_body_size 100M;

    location = / {
        rewrite ^ /index.php/site/$host break;
    }

...

但它根本不起作用......

你能幫助我嗎? :D

谢谢

相关内容