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

Nginx配置:全站HTTP转HTTPS重定向如何例外指定页面

修改Nginx配置实现指定HTTP页面不重定向

要实现仅让http://example.com/book(及其子路径)通过HTTP加载,其他HTTP请求仍重定向至HTTPS,只需修改监听80端口的server块,新增针对/book路径的处理规则,具体配置如下:

修改后的完整配置

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

    # 处理/book路径的HTTP请求,不进行重定向
    location /book {
        root /var/www/html/example.com;
        index index.php;
        try_files $uri $uri/ /index.php?$args;

        # 处理该路径下的PHP文件
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/run/php-fpm/www.sock;
            fastcgi_index   index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

    # 其他所有HTTP请求重定向至HTTPS
    location / {
        return 301 https://example.com$request_uri;
    }
}

server {
    listen 443 ssl http2;
    server_name example.com;

    root /var/www/html/example.com;
    index index.php;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php-fpm/www.sock;
        fastcgi_index   index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

关键配置说明

  • 针对/book的location规则:在80端口的server块中新增该规则,复用了HTTPS块中处理静态文件和PHP的逻辑,确保/book路径下的请求能直接在HTTP协议下正常加载。
  • 精确匹配可选:如果仅需让http://example.com/book单个页面(不含子路径)通过HTTP加载,可将location /book改为location = /book。
  • 其他请求重定向:保留默认的location /规则,确保除/book以外的所有HTTP请求仍自动重定向到HTTPS。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 16:07:00