簡體   English   中英

Symfony2 Nginx auth_basic配置不打開Web資產

[英]Symfony2 Nginx auth_basic config don't open web assests

我正在使用Nginx的Symfony2開發一個項目。

項目位於我的子域中,例如development_site.mysite.com。

我想在不進行身份驗證的情況下限制對該子域的訪問。 不僅用於開發和配置文件,還用於生產。

因此,我將sym_fony官方網站推薦的auth_basic組件添加到了nginx配置中location/扇區中的nginx配置文件中。 結果,在頁面加載服務器要求認證並加載所有內容之前,除了存儲在/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;
}

我自己解決了這個問題。

兩個錯誤:

  1. 句法錯誤
  2. auth_basic塊的位置不正確

語法錯誤在var/www/developing_site/.lock/.htpasswd; 我使用相對鏈接而不是絕對鏈接。 正確的格式是/var/www/developing_site/.lock/.htpasswd; (對不起...)

當我將auth_basic塊放置在location/我僅將身份驗證設置為實際上處理所有/web請求的/ location ...(由於第一個錯誤,未處理/ web請求...)

主要symfony請求由nginx配置文件中的location ~ ^/(app_dev|config)\\.php(/|$)塊處理。

解決方案:要在不進行身份驗證的情況下auth_basic任何請求的請求限制在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;
}

暫無
暫無

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

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