密码保护Nginx服务器上的wordpress网站的子文件夹

时间:2017-01-22 09:34:54

标签: php wordpress nginx

我面临一个奇怪的问题。 我在nginx服务器上为wp-admin启用了密码保护 但是一旦我启用密码保护,wp-admin网页的php解析就会停止。

意思是,输入用户名和密码后,我可以下载php文件源代码而不是用登录界面查看已处理的HTML。

这是我的配置文件 请告诉我,我做错了什么?

server {

    ... server stuff here ..

    # Wordpress Rules
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ^~ /wp-admin/ {
         auth_basic "Restricted Access";
         auth_basic_user_file /home/user/domains/.htpasswd;
    }

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

    location ~ /\.ht {
        deny all;
    }

    # Cache control
    location ~*  \.(jpg|jpeg|png|gif|ico|svg|ttf)$ {
        expires 365d;
    }
    location ~*  \.(css|js)$ {
        expires 30d;
    }

    # Enable "Vary-Accept-Encoding" headers
    # They are enabled in the nginx.conf
}

如果我删除密码保护,一切正常,管理面板也可以正常工作。我想我没有正确添加密码块。

1 个答案:

答案 0 :(得分:1)

您可能需要阅读how nginx processes a request

问题是location ^~ /wp-admin/块无法处理PHP文件。

这可能有效,但请尝试使用嵌套块:

location ^~ /wp-admin/ {
    auth_basic "Restricted Access";
    auth_basic_user_file /home/user/domains/.htpasswd;

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

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