簡體   English   中英

ubuntu 上 symfony 的 Nginx 配置

[英]Nginx config for symfony on ubuntu

我想用 nginx 設置 symfony 並且這個配置工作正常

server {
    listen 80;
    root /src/web;

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

    location ~ ^/app\.php(/|$) {
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/app.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    error_log /var/log/nginx/symfony_error.log;
    access_log /var/log/nginx/symfony_access.log;
}

但是我也希望在我的服務器上我也應該能夠通過 app_dev.php 和 app_test.php 訪問文件

到目前為止,上面的配置。 http://127.0.0.1/api/check工作正常

但我也想要

http://127.0.0.1/app_dev.php/api/checkhttp://127.0.0.1/app_test.php/api/check能正常工作。

目前它給了我 404 錯誤

假設您使用它進行開發,您可以在capturing group ()列出每個文件(環境()並用|分隔| 這基本上意味着“或”。

location ~ ^/(app|app_dev|app_test)\.php(/|$) {
    ....
}

不要在生產服務器上使用這個例子很重要,因為它是完全不安全的

在生產服務器上,您當前的conf僅允許app.php是正確的。

有關更多信息,您可以使用來自symfonynginx官方文檔的描述。

如您所見,您應該在 nginx conf 文件中添加一些行:

location ~ ^/(app_dev|config)\.php(/|$) {
    ...
}

如果您還需要 app_test.php,則應該對其進行一些更改。 只需包含文件名:

location ~ ^/(app_dev|app_test)\.php(/|$) {
    ...
}

但請注意:

在生產中,不要包含這個,也不要部署 app_dev.php 或 app_test.php

此外,最好在error_log ...之前進行以下配置:

location ~ \.php$ {
    return 404;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM