【问题标题】:NGINX: Rewrite using http without redirectionNGINX:使用http重写而不重定向
【发布时间】:2018-11-27 01:51:08
【问题描述】:

叠花花, 我需要创建一个指向http://example.com 的反向代理,但不是将它重定向到我需要重写它的站点,而不是更改 URL。我的 nginx 反向代理的 IP 是http://10.31.0.147/。 不幸的是,我当前的配置不起作用:

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        location / {
        rewrite ^/$ http://example.com break;
        proxy_redirect off;
 }
}

如果我从重写命令中删除 http://,我会收到错误“404 Not Found”如果我保持原样,我会被重定向,并且 URL 会更改为 www.example.com。有没有办法解决这个问题而不改变http://10.31.0.147。在 NGINX 文档中,我读到每当我使用“http://”时,页面都会被重定向。

【问题讨论】:

  • 为什么不使用 proxy_pass?

标签: http redirect nginx url-rewriting


【解决方案1】:

我没有使用重写,而是使用了代理通行证

    location / {
       proxy_pass   https://example.com;
       proxy_set_header X-Real-IP example.com;
       proxy_set_header X-Forwarded-For example.com;
       proxy_set_header Host example.com;
    }

【讨论】:

    【解决方案2】:

    很简单:

    location / {
      proxy_pass https://$server_name$request_uri;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
      proxy_set_header Host $http_host;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Port $server_port;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-11-11
      • 2011-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 2015-01-08
      • 2017-02-06
      相关资源
      最近更新 更多