我正在使用Symfony2,Nginx开发一个项目。
项目位于我的子域中,如developing_site.mysite.com。
我想在没有认证的情况下限制对这个子域的访问。不仅是开发和配置文件,也是生产。
因此,我将auth_basic组件添加到nginx的location/扇区中的nginx文件中,这是symfony官方网站推荐的。因此,在页面加载服务器进行身份验证和加载所有内容之前,除了存储在/web目录中的任何文件(如图像、js、css等)外,其他所有内容都要加载。因此,.php处理的所有内容都没有任何样式和动态功能。
那么我该如何解决这个问题呢?我做错什么了?
Nginx配置如下所示:
server {
listen {MyServerIp};
server_name developing_site.mysite.com;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
try_files $uri /app.php$is_args$args;
auth_basic "Restricted Content";
auth_basic_user_file var/www/developing_site/.lock/.htpasswd;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# 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;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# 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/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}发布于 2016-04-20 14:36:09
我自己解决了这个问题。
两个错误:
auth_basic块的不正确位置语法错误出现在var/www/developing_site/.lock/.htpasswd;中。我用的是相对链接而不是绝对链接。正确的表格是/var/www/developing_site/.lock/.htpasswd; (对不起.)
当我将auth_basic块放置在location/中时,我只将身份验证设置为/位置,该位置实际上处理所有/web请求.(/web请求没有被处理是因为第一次错误.)
主symfony请求由nginx文件中的location ~ ^/(app_dev|config)\.php(/|$)块处理。
解决方案:若要将任何请求限制为任何未经身份验证的developing_site.mysite.com文件,auth_basic块应放在任何location块之前。
因此,正确的nginx配置应该如下所示:
server {
listen MyServerIp;
server_name developing_site.mysite.com;
auth_basic "Unauthorized";
auth_basic_user_file /var/www/.lock/.htpasswd;
root /var/www/developing_site/web;
index index.php index.html index.htm;
location / {
try_files $uri /app.php$is_args$args;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# 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;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
# 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/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}https://stackoverflow.com/questions/36745354
复制相似问题