You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Azure App Service站点正确配置nginx.conf?解决404错误

Azure App Service Linux(Nginx+PHP)404问题修复与配置梳理

当前配置的核心问题

  • Root路径错误:你把root设置为/home/site/wwwroot/themes/mywebsite.com,导致Nginx将主题文件夹认定为网站根目录,但PHP应用的核心路由、控制器文件通常在/home/site/wwwroot下,因此除了主题内的首页,其他页面的PHP路径均无法匹配,引发404。
  • 冗余静态资源Location:单独配置的/css/、/js/、/images/节点完全多余——root已指定根目录,Nginx会自动在根路径下查找这些子目录;且这些节点与后续静态文件正则location冲突,可能导致缓存规则不生效。
  • HTTPS配置缺失关键项:listen 443 ssl块未配置ssl_certificate和ssl_certificate_key,不过Azure App Service会在前端负载层处理SSL终止,实际可改为监听平台默认的8080端口。

修复后的配置建议

server {
    listen 80;
    server_name mywebsite.com www.mywebsite.com;

    return 301 https://$host$request_uri;
}

server {
    # Azure App Service Linux 默认Nginx监听8080端口,平台会转发HTTPS请求至此
    listen 8080;
    server_name mywebsite.com www.mywebsite.com;

    # 网站根目录指向PHP应用核心目录
    root /home/site/wwwroot;
    index index.php index.html;

    # WWW域名跳转至非WWW
    if ($host ~* ^www\.(.+)) {
        return 301 https://$1$request_uri;
    }

    # 主路由规则:优先匹配静态文件,找不到则交由index.php处理(适配PHP框架路由)
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # PHP-FPM处理规则:确保文件存在再转发,避免无效请求触发错误
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        try_files $uri =404;
    }

    # 主题静态资源缓存规则:匹配主题内的静态文件目录
    location ~ ^/themes/mywebsite/(css|js|images)/ {
        expires 1M;
        add_header Cache-Control "public, immutable";
        add_header Access-Control-Allow-Origin *;
    }

    # 通用静态文件缓存规则
    location ~* \.(ttf|otf|eot|woff|woff2|css|js|jpg|gif|png|pdf|swf|svg|svgz|ico|webp)$ {
        expires 1M;
        add_header Cache-Control "public, immutable";
        add_header Access-Control-Allow-Origin *;
    }

    error_log /home/LogFiles/nginx/error.log warn;
    access_log /home/LogFiles/nginx/access.log combined;
}

Azure App Service Linux PHP栈默认Nginx配置参考

若需恢复官方默认配置,以下是/etc/nginx/sites-available/default的原始内容:

server {
    listen 8080;
    listen [::]:8080;
    root /home/site/wwwroot;
    index  index.php index.html index.htm;
    server_name  localhost;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # 错误页跳转
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # PHP请求转发规则
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # 禁止访问.htaccess文件
    location ~ /\.ht {
        deny  all;
    }
}

额外注意事项

  • 自定义配置部署后,需重启App Service生效:可在Azure门户控制台执行service nginx restart,或直接使用门户的「重启」按钮。
  • 检查静态资源权限:确保/home/site/wwwroot/themes/mywebsite.com/下的文件/目录对www-data用户可读。
  • 查看错误日志:/home/LogFiles/nginx/error.log中的详细报错可帮助定位具体路径问题。

内容的提问来源于stack exchange,提问作者V1xIII

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 15:44:53