Nginx配置问题求助:/blog/可访问但子路径返回404
解决Bludit博客在Nginx子路径/blog下的404问题
你的问题核心在于/blog/路径的请求转发规则配置错误,导致动态路由无法正确解析到Bludit的入口文件。以下是修正后的完整配置及关键修改说明:
关键修改点
- 修正
try_files的 fallback 路径:原配置中/index.php?$args会指向主站根目录的index.php,应改为/blog/index.php?$args,让子路径请求正确转发到Bludit的入口文件 - 清理冗余的无效规则:删除未做任何处理的
location ~ ^/bl-content/(databases|workspaces|pages|tmp)/.*$ { }规则 - 优化PHP请求的路径解析:确保
SCRIPT_FILENAME正确指向Bludit目录下的PHP文件
修正后的完整Nginx配置
## # This is the main Nginx configuration file. # It should typically be located at /etc/nginx/nginx.conf # This file includes configuration snippets from other files. # Those files are located in /etc/nginx/conf.d/. ## # Default server configuration server { # SSL configuration listen [::]:443 ssl ipv6only=on; # Listen for IPv6 connections on port 443 listen 443 ssl; # Listen for IPv4 connections on port 443 server_name insurancepadosi.in www.insurancepadosi.in; # Your domain names # SSL certificate configuration ssl_certificate /etc/letsencrypt/live/insurancepadosi.in/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/insurancepadosi.in/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; # SSL configuration options ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # Diffie-Hellman parameter for SSL # Root directory for the main website root /var/www/html/public; # Default index files index index.html index.htm index.nginx-debian.html index.php; # Main location block for handling requests to the root location / { # Try serving the requested file or directory, # fallback to displaying a 404 error if not found try_files $uri $uri.html $uri/ =404; } # Location block for serving the blog at /blog location /blog/ { alias /var/www/html/bludit/; index index.php; # 修正fallback路径,指向blog子路径的index.php try_files $uri $uri/ /blog/index.php?$args; location ~ \.php$ { include snippets/fastcgi-php.conf; # 确保SCRIPT_FILENAME正确指向alias目录下的PHP文件 fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; } } # Bludit安全防护规则 location ^~ /blog/bl-content/databases/ { deny all; } location ^~ /blog/bl-content/workspaces/ { deny all; } location ^~ /blog/bl-content/pages/ { deny all; } location ^~ /blog/bl-kernel/ { deny all; } # 禁止访问.htaccess文件 location ~ /\.ht { deny all; } } # HTTP转HTTPS配置 server { listen 80 default_server; # Listen for HTTP connections on port 80 listen [::]:80 default_server; # Listen for HTTP IPv6 connections on port 80 server_name insurancepadosi.in www.insurancepadosi.in; # Your domain names # Redirect all HTTP traffic to HTTPS return 301 https://$host$request_uri; }
生效步骤
- 执行
nginx -t验证配置语法是否正确 - 验证通过后执行
systemctl reload nginx重新加载配置 - 确保
/var/www/html/bludit目录的权限正确,Nginx进程(通常为www-data用户)需要拥有读取权限
内容的提问来源于stack exchange,提问作者tejesh
相关产品推荐
相关产品推荐

