如何在nginx的子目录中安装symfony2应用程序 [英] How to install symfony2 app in a subdirectory in nginx

查看:200
本文介绍了如何在nginx的子目录中安装symfony2应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在同一个主机上但在不同的子目录(或位置块)上安装多个symfony2应用程序。



使用此配置nginx抛出

$ p> / login - > / base / login
/ app1 - > / base / app1
/ app2 - > / base / app2

当前配置

  root / base / default; #指向空目录

#登录应用程序
位置^〜/ login {
alias / base / login / web;
try_files $ uri app_dev.php;
}

#任何其他
位置〜^ /([\w\ - ] +){
alias / base / $ 1 / web;
try_files $ uri app_dev.php;
}

location / {
#重定向到登录
rewrite ^ / login redirect;
}

#句柄PHP
位置〜\.php $ {
include fastcgi_params;
fastcgi_split_path_info ^(。+ \.php)(/.+)$;
fastcgi_param PATH_INFO $ fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $ document_root $ fastcgi_path_info;
fastcgi_param HTTPS off;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}


解决方案

(sf2 doc不解释如何cgi参数需要和解释,你需要通过Request.php来理解),所以我分享这个。



这是一个config它似乎确定与sf2在目录{subdir}(和web访问其他文件比{subdir} / web / *禁止)。



它适用于我



当然,用/ path / from / docroot / to替换{subdir}可以通过向{subdir}添加dev来选择
$ b

dev环境(因为url中的app_dev.php不再适用于此conf)

 服务器{

#一般指令

位置〜^ / {subdir}(/.*)$ {
try_files / {subdir} / web $ 1 @ sf2;
}
位置〜^ / {subdir} dev(/.*)$ {
expires off;
try_files / {subdir} / web $ 1 @ sf2dev;
}
location @ sf2 {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root / {subdir} /web/app.php;
fastcgi_param SCRIPT_NAME /{subdir}/app.php;
fastcgi_param REQUEST_URI / {subdir} $ 1;
}
location @ sf2dev {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root / {subdir} /web/app_dev.php;
fastcgi_param SCRIPT_NAME /{subdir}dev/app_dev.php;
fastcgi_param REQUEST_URI / {subdir} dev $ 1;
}


#其他位置指令

#如果一些其他应用程序需要php,把你的通常位置cactch php here

}



我希望它有帮助(没有任何配置错误)保证...



当然,如果你不需要,你可以选择prod / dev conf。你可以使用var和只有一个@ sf2位置:

  set $ sf2_root / {subdir}; 
location〜^ / {subdir}(/.*)$ {
set $ sf2_prefix / {subdir};
set $ sf2_ctrl app.php;
try_files $ sf2_root / web $ 1 @ sf2;
}
location〜^ / {subdir} dev(/.*)$ {
set $ sf2_prefix / {subdir} dev;
set $ sf2_ctrl app_dev.php;
expires off;
try_files $ sf2_root / web $ 1 @ sf2;
}
location @ sf2 {
expires off;
fastcgi_pass {your backend};
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ sf2_root / web / $ sf2_ctrl;
fastcgi_param SCRIPT_NAME $ sf2_prefix / $ sf2_ctrl;
fastcgi_param REQUEST_URI $ sf2_prefix $ 1;
}


I need to install multiple symfony2 applications on the same host but on different subdirectories (or location blocks).

With this config nginx throws a "file not found" or redirect loop message when trying to access any url.

Example:

/login -> /base/login
/app1 -> /base/app1
/app2 -> /base/app2

Current Config:

root /base/default; #Points to an empty directory

# Login Application
location ^~ /login {
    alias /base/login/web;
    try_files $uri app_dev.php;
}

# Anything else
location ~ ^/([\w\-]+) {
    alias /base/$1/web;
    try_files $uri app_dev.php;
}

location / {
    # Redirect to the login
    rewrite ^ /login redirect;
}

# Handle PHP
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    fastcgi_param HTTPS off;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
}

解决方案

After hours spended to find this (sf2 doc doesn't explain how cgi parameters are needed and interpreted, you need to go through Request.php to understand), so I share this.

This is a config which seems ok with sf2 in a directory {subdir} (and web access to others files than {subdir}/web/* prohibited).

It works for me with php-fpm (socket).

Of course, replace "{subdir}" by your /path/from/docroot/to/symfony_root/

dev environnement can be choosen by adding "dev" to "{subdir}" (since app_dev.php in the url no longer works with this conf)

server {

  # general directives

  location ~ ^/{subdir}(/.*)$ {   
    try_files /{subdir}/web$1 @sf2;
  }
  location ~ ^/{subdir}dev(/.*)$ {
    expires off;
    try_files /{subdir}/web$1 @sf2dev;
  }
  location @sf2 {
    expires off;
    fastcgi_pass   {your backend};
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app.php;
    fastcgi_param SCRIPT_NAME       /{subdir}/app.php;
    fastcgi_param REQUEST_URI       /{subdir}$1;
  }
  location @sf2dev {
    expires off;
    fastcgi_pass   {your backend};
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/{subdir}/web/app_dev.php;   
    fastcgi_param SCRIPT_NAME       /{subdir}dev/app_dev.php;       
    fastcgi_param REQUEST_URI       /{subdir}dev$1;     
  }


  # other location directives

  # if some others apps needs php, put your usual location to cactch php here

}

I Hope it helps (and there isn't any misconfiguration), given without any guarantee...

Of course you can pick out prod/dev conf if you don't need. And you can use var and only one @sf2 location instead :

  set $sf2_root /{subdir};
  location ~ ^/{subdir}(/.*)$ {   
    set $sf2_prefix /{subdir};  
    set $sf2_ctrl app.php;
    try_files $sf2_root/web$1 @sf2;
  }
  location ~ ^/{subdir}dev(/.*)$ {
    set $sf2_prefix /{subdir}dev;
    set $sf2_ctrl app_dev.php;
    expires off;
    try_files $sf2_root/web$1 @sf2;
  }
  location @sf2 {
    expires off;
    fastcgi_pass   {your backend};
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$sf2_root/web/$sf2_ctrl;
    fastcgi_param SCRIPT_NAME       $sf2_prefix/$sf2_ctrl;
    fastcgi_param REQUEST_URI       $sf2_prefix$1;
  }

这篇关于如何在nginx的子目录中安装symfony2应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆