nginx服务器配置限制访问88.html,实际效果提示输入密码验证后,页面空白

2024-07-02 10:47 nginx服务器配置限制访问88.html,实际效果提示输入密码验证后,页面空白已关闭评论

配置Nginx来限制对由WordPress生成的88.html页面的访问。下面是一个配置示例,它限制对88.html页面的访问,并正确处理PHP请求。

生成有效的 .htpasswd 文件。当前WordPress站点根目录是 /var/www/wordpress

Nginx 配置示例

  1. 打开你的网站配置文件:
sudo nano /etc/nginx/sites-available/teachcourse.cn.conf
  1. 添加以下配置:
server {
    listen 80;
    server_name www.teachcourse.cn;

    root /var/www/wordpress;  # 你的WordPress根目录
    index index.php index.html index.htm;

    # 限制对特定文件88.html的访问
    location = /88.html {
        auth_basic "Restricted Area";
        auth_basic_user_file /etc/nginx/.htpasswd;

        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;  # 根据你的PHP-FPM版本和配置进行修改
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    # 处理所有php请求
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;  # 根据你的PHP-FPM版本和配置进行修改
    }

    # 处理静态文件
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }

    # 处理wordpress其他固定链接和请求
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

解释配置

  1. auth_basicauth_basic_user_file: 限制访问 88.html,要求输入用户名和密码。
  2. try_files $uri =404: 确保如果 88.html 文件不存在,返回404错误,而不是尝试其他处理方式。
  3. include fastcgi_paramsfastcgi_pass: 处理 PHP 请求,确保 Nginx 正确地与 PHP-FPM 通信。
  4. 处理其他 PHP 请求: 使用 location ~ \.php$ 来处理所有 PHP 文件的请求,确保 WordPress 其他页面正常工作。
  5. 处理静态文件: 使用 location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ 来处理静态文件请求,并设置缓存策略。
  6. 处理 WordPress 其他固定链接和请求: 使用 location /try_files $uri $uri/ /index.php?$args; 确保所有 WordPress 固定链接和请求都能正确地处理。

重启 Nginx

保存配置文件并重启 Nginx:

sudo nginx -t  # 检查配置是否正确
sudo systemctl restart nginx

通过这些步骤,应该能够实现对 88.html 页面访问的限制,并确保 WordPress 的其他功能正常运行。

运行效果

2024/07/02 10:24:45 [error] 28057#28057: *2 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 119.131.138.239, server: teachcourse.cn, request: "GET /88.html HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "www.teachcourse.cn"
2024/07/02 10:31:16 [notice] 30651#30651: signal process started

收到404错误和“Primary script unknown”的日志提示,意味着Nginx在处理88.html的请求时未能找到正确的脚本文件。考虑到88.html是由WordPress动态生成的页面,配置需要进行一些调整以正确处理该请求。

修改配置文件

  1. 打开Nginx配置文件:
sudo nano /etc/nginx/sites-available/your_site.conf
  1. 修改配置以正确处理WordPress动态生成的页面:
server {
    listen 80;
    server_name www.teachcourse.cn;

    root /var/www/wordpress;  # 你的WordPress根目录
    index index.php index.html index.htm;

    # 限制对特定文件88.html的访问
    location = /88.html {
        auth_basic "Restricted Area";
        auth_basic_user_file /etc/nginx/.htpasswd;

        # 由于88.html是WordPress动态生成的页面,需要通过index.php处理
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param QUERY_STRING $query_string;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;  # 根据你的PHP-FPM版本和配置进行修改
    }

    # 处理所有php请求
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;  # 根据你的PHP-FPM版本和配置进行修改
    }

    # 处理静态文件
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 365d;
    }

    # 处理wordpress其他固定链接和请求
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
}

配置说明

  1. auth_basicauth_basic_user_file: 限制访问 88.html,要求输入用户名和密码。
  2. fastcgi_param SCRIPT_FILENAME $document_root/index.php: 将请求的处理指向 index.php,因为 88.html 是由WordPress动态生成的。
  3. fastcgi_param QUERY_STRING $query_string: 将查询字符串传递给PHP处理。

重启Nginx

保存配置文件并重启Nginx:

sudo nginx -t  # 检查配置是否正确
sudo systemctl restart nginx

检查文件权限

确保 /var/www/wordpress 目录及其文件具有正确的权限:

sudo chown -R www-data:www-data /var/www/wordpress
sudo chmod -R 755 /var/www/wordpress

通过这些步骤,希望应该能够解决 "Primary script unknown" 的错误,并正确地限制对88.html页面的访问。

当前文章价值1.75元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

你可能感兴趣的文章

来源:每日教程每日一例,深入学习实用技术教程,关注公众号TeachCourse
转载请注明出处: https://teachcourse.cn/3564.html ,谢谢支持!

资源分享

如何使用手机支付给羊城通充值 如何使用手机支付给羊城通充值
json格式化工具 json格式化工具
Python库JWT实现token校验的示例 Python库JWT实现token校验的
带www和不带www域名与网站收录量、权重关系 带www和不带www域名与网站收录量

评论已关闭!