简体   繁体   English

Nginx Symfony2 暂存配置

[英]Nginx Symfony2 Staging Config

i'm trying to configure nginx to serve several releases of one symfony project.我正在尝试将 nginx 配置为为一个 symfony 项目的多个版本提供服务。

Releasefolder structure:发布文件夹结构:
/var/www/my-project/releases/24/ /var/www/my-project/releases/24/
/var/www/my-project/releases/46/ /var/www/my-project/releases/46/
/var/www/my-project/releases/47/ /var/www/my-project/releases/47/

I'd like to call a URL like "http://my-server/my-project/release/47" which should access /var/www/my-project/releases/47/web/app.php on the server.我想调用一个像“http://my-server/my-project/release/47”这样的 URL,它应该访问服务器上的 /var/www/my-project/releases/47/web/app.php .

I tried for a while now but couldn't find the final solution, any help would be great!我现在尝试了一段时间,但找不到最终解决方案,任何帮助都会很棒!

My last try:我的最后一次尝试:

server {
    server_name my-server;

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

    location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
        root /var/www/my-project/releases/$1/web;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/app.php;
        fastcgi_param HTTPS off;

        error_log /var/log/nginx/my-project_$1_error.log;
        access_log /var/log/nginx/my-project_$1_access.log;

        # 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;
    }
}

This config produces following error:此配置产生以下错误:

[error] 2272#0: *169 rewrite or internal redirection cycle while internally redirecting to "/my-project/release/47app.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.php" [错误] 2272#0:*169 重写或内部重定向循环,同时内部重定向到“/my-project/release/47app.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.phpapp.php”

You are missing a root directive in your server container -- is /var/www the default on your architecture?您的server容器中缺少root指令—— /var/www是您架构的默认设置吗? Otherwise, add a root directive:否则,添加一个根指令:

root /var/www;

try_files is failing for find a match and will rewrite the URL by appending app.php to it. try_files无法找到匹配项,并将通过向其附加app.php来重写 URL。 The resulting URL does not match your other location block, so it gets rewritten again and again.生成的 URL 与您的其他位置块不匹配,因此它会一次又一次地被重写。

The final term on your try_files directive is wrong. try_files指令的最后一个术语是错误的。 You have $2 where /web/ should be (probably inherited from a previous experiment).你有$2应该放在/web/地方(可能是从之前的实验中继承的)。

Using try_files to make a general rewrite is a bad idea, as any mistyped URL could lead to a rewrite loop.使用try_files进行一般重写是一个坏主意,因为任何错误输入的 URL 都可能导致重写循环。 Try:尝试:

location / {
  rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
}

I changed the config to this:我把配置改成这样:

server {
    server_name my-server;
    root /var/www;

    location / {
        rewrite ^(/my-project/release/[-a-zA-Z0-9]+)$ $1/app.php;
    }

    location ~ /my-project/release/([-a-zA-Z0-9]+)/app\.php(/|$) {
        alias /var/www/my-project/releases/$1/web;

        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/app.php;
        fastcgi_param HTTPS off;

        error_log /var/www/my-project/releases/$1/app/logs/nginx_error.log;
        access_log /var/www/my-project/releases/$1/app/logs/nginx_access.log;

        # 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;
    }
}

Now a call like " http://my-server/my-project/release/47 " points correctly to "/var/www/my-project/releases/47/web/app.php".现在像“ http://my-server/my-project/release/47 ”这样的调用正确指向“/var/www/my-project/releases/47/web/app.php”。

The problem is now that the url "my-project/release/47" arrives at symfony router which can't find a route to this url.现在的问题是 url "my-project/release/47" 到达 symfony 路由器,它找不到到这个 url 的路由。

What is now the right way to keep the url showing up like "//my-server/my-project/release/47" at browser but arrives at symfony as "//my-server/"?现在让 url 在浏览器中显示为“//my-server/my-project/release/47”但到达 symfony 为“//my-server/”的正确方法是什么?

Thank you all for your help, unfortunately i couln't get it working.谢谢大家的帮助,不幸的是我无法让它工作。 I think i have to learn more about nginx configuration generally to be able to handle such problems, before i can use it in production.我想我必须了解更多关于 nginx 配置的信息才能处理这些问题,然后才能在生产中使用它。

My workaround for my problem is to create an own nginx conf per release.我的问题的解决方法是为每个版本创建一个自己的 nginx conf。 The file will be created at build process, is then transfered to the webserver, copied to /etc/nginx/conf.d/[releasename].conf and activated by sudo service nginx reload .该文件将在构建过程中创建,然后传输到网络服务器,复制到/etc/nginx/conf.d/[releasename].conf并由sudo service nginx reload激活。

A deinstallation script within the release dir removes sources and config, when release no longer needed.当不再需要发布时,发布目录中的卸载脚本会删除源和配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM