【问题标题】:nginx server configuration returns php codenginx服务器配置返回php代码
【发布时间】:2017-08-02 19:42:54
【问题描述】:

我有一个 nginx 服务器,一切似乎都可以正常工作,但是当我为目录添加身份验证时,服务器返回 php 代码作为下载。

server {
listen 80 default_server;
listen [::]:80 default_server;

listen 443 ssl;

root /var/www/html;
index index.php index.html index.htm;

server_name _;

location ^~ /auth/ {
    try_files $uri $uri/ =404;
    auth_basic "Auth";
    auth_basic_user_file /etc/nginx/.htpasswd;
}

location / {
    try_files $uri $uri/ =404;
}

location ~ /\. {
    deny  all;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ /(config\.php|common\.php|cache|files|images/avatars/upload|includes|store) {
    deny all;
    return 403;
}

location ~* \.(gif|jpe?g|png|css)$ {
    expires   30d;
}

add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;

add_header Strict-Transport-Security 'max-age=31536000; preload';
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://assets.zendesk.com; font-src 'self' https://themes.googleusercontent.com; frame-src https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'";

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.4.4 8.8.8.8 valid=300s;
resolver_timeout 10s;

location ~ /\.ht {
    deny all;
}
}

没有位置 ^~ /auth/ 一切正常。 我在不同的浏览器中都遇到过这种情况。

【问题讨论】:

    标签: php authentication nginx configuration webserver


    【解决方案1】:

    nginx processes a request 通过选择一个位置。新的location 不包含执行 PHP 所需的代码。您应该添加一个nested location block 来处理/auth/ 目录中的PHP。

    location ^~ /auth/ {
        try_files $uri $uri/ =404;
        auth_basic "Auth";
        auth_basic_user_file /etc/nginx/.htpasswd;
    
        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
    }
    ...
    location ~ \.php$ { ... }
    

    【讨论】:

      猜你喜欢
      • 2012-12-23
      • 2017-05-12
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 2014-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多