【问题标题】:nginx rewrite mystery - duplicating hostname and losing httpsnginx 重写之谜 - 复制主机名并丢失 https
【发布时间】:2010-11-24 20:34:22
【问题描述】:

我正在开发服务器上用 nginx 替换 lighttpd。我让它与 PHP 和 SSL 一起工作,但我对应该是一个简单的重写感到困惑。我需要从

重写 URL
http[s]://dev.foo.com/signup/123456

http[s]://dev.foo.com/signup/index.php?attcode=123456

我使用的规则是:

rewrite ^/signup/([0-9]+)$  /signup/index.php?attycode=$1 last;

我已经尝试了很多变化,移动它,把它放在一个位置块内。发生的情况是 URL 被重写为:

http://dev.foo.com/dev.foo.com/signup/123456

主机名插入了,好像总是丢https,去http。

我的 nginx.com 服务器部分如下。我已经阅读并重新阅读了 nginx 文档(原样)并搜索了 nginx 邮件列表,但我尝试过的都没有解决这个问题。

Ubuntu 8.0.4 LTS 以防万一。

谢谢。


server {
    listen      80;
    listen      443 default ssl;
    server_name dev.foo.com   dev.bar.com   localhost;
    root        /var/www/foo;
    index       index.php index.html;
    # ssl cert stuff omitted
    charset utf-8;
    access_log  /var/log/www/dev.access.log  main;

    location ~ /\. {
        deny  all;
    }   

    location ~* ^.+\.(inc|tpl|sql|ini|bak|sh|cgi)$ {
        deny all;
    }   

    location ~* ^/(scripts|tmp|sql)/ {
        deny all;
    }   

    rewrite ^/robots.txt$         /robots_nocrawl.txt break;
    rewrite ^/signup/([0-9]+)$    /signup/index.php?attycode=$1 last;

    location / { 
        try_files $uri $uri/ /error_404.php;
    }   

    location ~ \.php$ {
        fastcgi_pass localhost:51115;
        fastcgi_index index.php;
        fastcgi_intercept_errors on; 
        include fastcgi_params;
        fastcgi_param SERVER_NAME       $http_host;
        fastcgi_param SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    }

    error_page  404              /error_404.php;
}

【问题讨论】:

    标签: nginx rewrite


    【解决方案1】:

    不要将 HTTP 和 HTTPS 放在同一个服务器块中。将它们分成两个几乎相同的服务器块,一个用于 HTTP,一个用于 HTTPS。否则你会混淆各种 Nginx 内部结构。

    【讨论】:

    • nginx 文档至少暗示在单个服务器块中组合 http 和 https 是可以的。见nginx.org/en/docs/http/configuring_https_servers.html
    • 我去掉了listen 80参数和额外的服务器名称来简化。重写仍然不起作用,并且仍然从 https 切换到 http。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-09
    • 2013-04-22
    • 2013-09-03
    • 2018-12-07
    • 2023-03-20
    • 2019-01-28
    相关资源
    最近更新 更多