2015-09-01 67 views
0

我想设置我的NGINX配置文件,以便只有拥有htpsasswd的用户可以在其浏览器中查看our-site.com/phpinfo.php限制phpinfo.php文件下载,而不是在浏览器中显示

在我的server block之内,我在我的/etc/nginx/sites-available/配置文件中定义了一个location block

# PHP: phpinfo() access restrictions 
location ~ ^\/phpinfo\.php$ { 
    auth_basic "Restricted website - authorised access only"; 
    auth_basic_user_file /etc/nginx/.htpasswd; 
} 

当我去our-site.com/phpinfo.php我提示我输入密码,但是当我输入该文件被下载而不是显示在浏览器中的phpinfo。

有谁知道如何解决这个问题?

这里是我的完整配置文件

server { 
    # Listen on port 80 as well as post 443 for SSL connections. 
    listen 80; 
    #listen 443 default ssl; 

    #server_name localhost; 
    server_name www.our-domain.com; 

    # Specify path to your SSL certificates. 
    #ssl_certificate /etc/nginx/certificates/yourcertificate.crt; 
    #ssl_certificate_key /etc/nginx/certificates/yourcertificate.key; 

    # Path to the files in which you wish to 
    # store your access and error logs. 
    #access_log /path/to/your/logs/access_log; 
    #error_log /path/to/your/logs/error_log; 

    # If the site is accessed via mydomain.com 
    # automatically redirect to www.magento.localhost.com. 
    #if ($host = 'production') { 
     #rewrite ^/(.*)$ http://www.production/$1permanent; 
    #} 

    root /var/www/production/; 

    location/{ 
     index index.html index.htm index.php; 
     try_files $uri $uri/ @handler; 
     auth_basic "Restricted website - authorised access only"; 
     auth_basic_user_file /etc/nginx/.htpasswd; 
    } 

    # Denies access to specific directories no one 
    # in particular needs access to anyways. 
    location /app/ { deny all; } 
    location /includes/ { deny all; } 
    location /lib/ { deny all; } 
    location /media/downloadable/ { deny all; } 
    location /pkginfo/ { deny all; } 
    location /report/config.xml { deny all; } 
    location /var/ { deny all; } 

    # PHP: phpinfo() access restrictions 
    location ~ ^\/phpinfo\.php$ { 
     auth_basic "Restricted website - authorised access only"; 
     auth_basic_user_file /etc/nginx/.htpasswd; 
    } 

    location /admin { 
     index index.html index.htm index.php; 
     try_files $uri $uri/ @handler; 
     auth_basic "Restricted website - authorised access only"; 
     auth_basic_user_file /etc/nginx/.htpasswd; 
    } 


    # Deny all attempts to access hidden files 
    # such as .htaccess, .htpasswd, etc... 
    location ~ /\. { 
     deny all; 
     access_log off; 
     log_not_found off; 
    } 

    # This redirect is added so to use Magentos 
    # common front handler when handling incoming URLs. 
    location @handler { 
     rewrite//index.php; 
    } 

    # Forward paths such as /js/index.php/x.js 
    # to their relevant handler. 
    location ~ .php/ { 
     rewrite ^(.*.php)/ $1 last; 
    } 

    ## 
    # Rewrite for versioned CSS+JS via filemtime 
    ## 
    location ~* ^.+\.(css|js)$ { 
     rewrite ^(.+)\.(\d+)\.(css|js)$ $1.$3 last; 
     expires 31536000s; 
     access_log off; 
     log_not_found off; 
     add_header Pragma public; 
     add_header Cache-Control "max-age=31536000, public"; 
    } 
    ## 
    # Aggressive caching for static files 
    # If you alter static files often, please use 
    # add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"; 
    ## 
    location ~* \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|ogv|otf|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|t?gz|tif|tiff|ttf|wav|webm|wma|woff|wri|xla|xls|xlsx|xlt|xlw|zip)$ { 
     expires 31536000s; 
     access_log off; 
     log_not_found off; 
     add_header Pragma public; 
     add_header Cache-Control "max-age=31536000, public"; 
    } 

     # Handle the exectution of .php files. 
    location ~ .php$ { 
     if (!-e $request_filename) { 
      rewrite//index.php last; 
     } 
     expires off; 

     # --PHP5-FPM CONFIG START (keep fastcgi_param HTTPS OFF)-- 
     #fastcgi_pass unix:/var/run/php5-fpm.sock; 
     ##fastcgi_param HTTPS $fastcgi_https; 
     #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     # --PHP5-FPM CONFIG START-- 

     # --HHVM CONFIG START-- 
     fastcgi_pass 127.0.0.1:9000; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include  fastcgi_params; 
     try_files $uri $uri/ @handler; 
     # --HHVM CONFIG END-- 

     fastcgi_param MAGE_RUN_CODE default; 
     fastcgi_param MAGE_RUN_TYPE store; 
     include fastcgi_params; 
    } 
} 

它运行与HHVM一个NGINX服务器Magento的应用程序Ubuntu的盒子。

+1

请告诉我们其余的配置。 – ceejayoz

+0

@ceejayoz,刚刚添加了它,谢谢 – Holly

+0

'位置〜\ .php $'的'include'内容也放到'location〜^ \/phpinfo \ .php $' – Deadooshka

回答

0

您的phpinfo.php完成后,位置块将不会滚降至location ~ .php$ { - 每个位置块都是独立的。您需要将各种fastcgi等规则复制到其中(或者为了便于维护,请将它们放入包含中)。

+0

我不确定你的意思。我将它改为'location〜^ \/phpinfo \ .php $ {include fastcgi_params; auth_basic“限制网站 - 仅限授权访问”; auth_basic_user_file /etc/nginx/.htpasswd; } '但仍然有相同的问题 – Holly

+0

@CreedBratton你需要整个事情 - 'fastcgi_pass','try_files'等。没有它们,没有任何东西告诉'phpinfo.php'块根本不会传递给PHP。 – ceejayoz

相关问题