Azure上Nginx配置优化:实现SEO友好链接问题求助
SEO友好链接配置修复方案(Azure + Nginx环境)
一、启动命令修正
你当前设置的启动命令存在路径错误:
cp /home/default /etc/nginx/sites-enabled/default; service nginx restart
Azure App Service中自定义Nginx配置文件默认存放路径为/home/site/nginx/default.conf,而非/home/default,修正后的启动命令:
cp /home/site/nginx/default.conf /etc/nginx/sites-enabled/default; systemctl restart nginx
(部分环境用service nginx restart也可生效,优先用systemctl保证兼容性)
二、Nginx配置修改(实现SEO友好链接)
要去掉.php后缀、生成SEO友好链接,需在现有配置基础上添加重写规则并调整PHP解析逻辑,完整配置如下:
server { #proxy_cache cache; #proxy_cache_valid 200 1s; listen 8080; listen [::]:8080; absolute_redirect off; root /home/site/wwwroot; index index.php index.html index.htm; #server_name sitename.azurewebsites.net; # 301重定向带.php的链接到无后缀格式,避免重复内容收录 rewrite ^/(.*)\.php$ /$1 permanent; location / { index index.php; # 优先匹配无后缀路径,不存在则尝试补全.php后缀,最后交给index.php处理 try_files $uri $uri/ $uri.php?$args; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /html/; } # 禁止访问.git目录 location ~ /\.git { deny all; access_log off; log_not_found off; } # 兼容无后缀请求的PHP解析规则 location ~ [^/]\.(php|)$ { fastcgi_split_path_info ^(.+?\.php)(|/.*)$; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param HTTP_PROXY ""; # 无后缀请求自动补全.php后缀,确保PHP-FPM正确解析 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name.php; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param QUERY_STRING $query_string; fastcgi_intercept_errors on; fastcgi_connect_timeout 300; fastcgi_send_timeout 3600; fastcgi_read_timeout 3600; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; } }
三、关键修改说明
- 重写规则:
rewrite ^/(.*)\.php$ /$1 permanent;实现旧链接到新SEO链接的301跳转,利于搜索引擎权重继承。 - try_files逻辑:加入
$uri.php?$args,访问无后缀路径时自动尝试匹配对应.php文件。 - PHP解析规则:修改匹配模式兼容无后缀请求,同时补全.php后缀确保PHP-FPM能正确处理请求。
四、验证步骤
- 将修改后的配置保存到Azure App Service的
/home/site/nginx/default.conf(目录不存在则手动创建/home/site/nginx)。 - 更新Azure「配置」-「常规设置」中的启动命令为修正版本。
- 重启App Service实例后测试:
- 访问
https://你的域名/page,应正常加载原page.php内容。 - 访问旧链接
https://你的域名/page.php,会自动301跳转到无后缀链接。
- 访问
内容的提问来源于stack exchange,提问作者Bos
相关产品推荐
相关产品推荐

