配置Nginx来限制对由WordPress生成的88.html页面的访问。下面是一个配置示例,它限制对88.html页面的访问,并正确处理PHP请求。
生成有效的 .htpasswd 文件。当前WordPress站点根目录是 /var/www/wordpress。
Nginx 配置示例
- 打开你的网站配置文件:
sudo nano /etc/nginx/sites-available/teachcourse.cn.conf
- 添加以下配置:
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;
}
}
解释配置
auth_basic和auth_basic_user_file: 限制访问88.html,要求输入用户名和密码。try_files $uri =404: 确保如果88.html文件不存在,返回404错误,而不是尝试其他处理方式。include fastcgi_params和fastcgi_pass: 处理 PHP 请求,确保 Nginx 正确地与 PHP-FPM 通信。- 处理其他 PHP 请求: 使用
location ~ \.php$来处理所有 PHP 文件的请求,确保 WordPress 其他页面正常工作。 - 处理静态文件: 使用
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$来处理静态文件请求,并设置缓存策略。 - 处理 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动态生成的页面,配置需要进行一些调整以正确处理该请求。
修改配置文件
- 打开Nginx配置文件:
sudo nano /etc/nginx/sites-available/your_site.conf
- 修改配置以正确处理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;
}
}
配置说明
auth_basic和auth_basic_user_file: 限制访问88.html,要求输入用户名和密码。fastcgi_param SCRIPT_FILENAME $document_root/index.php: 将请求的处理指向index.php,因为88.html是由WordPress动态生成的。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元,扫一扫支付后添加微信提供帮助!(如不能解决您的问题,可以申请退款)

评论已关闭!