【问题标题】:NGINX return 403 when proxy_pass to http from https当proxy_pass从https到http时,NGINX返回403
【发布时间】:2022-07-07 01:11:44
【问题描述】:

需要有关有时可以工作的 proxy_pass 的帮助(nginx 版本:nginx/1.21.6)。

我构建了一个 RESTful Web 并在本地运行它。它运行良好,没有任何问题。 我可以通过“http://localhost:7000”访问所有功能。

然后,我配置一个 NGINX 服务器来在本地模拟 https。服务器配置是

http {
  server_names_hash_bucket_size   64;
  include                         mime.types;
  default_type                    application/octet-stream;

  sendfile                        on;

  keepalive_timeout               65;
  expires                         -1;
  server {
    server_name                   my-service.foo.com;
    rewrite                       ^(.*) https://my-service.foo.com$1 permanent;
  }
  server {
    listen                        443 ssl;
    ssl_certificate               /opt/local/etc/nginx/myservice.crt;
    ssl_certificate_key           /opt/local/etc/nginx/myservice.key;
    ssl_ciphers                   HIGH:!aNULL:!MD5;
    server_name                   my-service.foo.com;
    proxy_set_header              Host $host;
    location / {
      proxy_pass                  http://localhost:7000;
    }
  }
}

当然,我配置 /etc/hosts 添加127.0.0.1 my-service.foo.com

奇怪的是,当直接使用http://localhost:7000 时,一切都很好。 但是,当使用https://my-service.foo.com 时,它会返回 403(我使用 Firefox)。 例如访问https://my-service.foo.com/welcome.html 时,它会正确加载welcome.html。但是对于welcome.html (<link rel="stylesheet" href="style.css">) 中的style.css 返回403。

而且,如果我刷新页面,它会为https://my-service.foo.com/welcome.html 返回 403。再次刷新它,正确获取welcome.html,但style.css 为403。 基本上,它会为welcome.htmlstyle.css 轮流返回403。

而且,当它返回 403 时,我在我的网络应用程序端找不到请求。 NGINX 似乎没有发送请求。

查看 NGINX 的 error.log 时,什么也没有。 access.log 只显示如下内容:

127.0.0.1 - - [01/Jun/2022:22:08:31 -0700] "GET /welcome.html HTTP/1.1" 403 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:08:32 -0700] "GET /welcome.html HTTP/1.1" 200 1881 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:08:32 -0700] "GET /style.css HTTP/1.1" 403 0 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:08:33 -0700] "GET /session/status HTTP/1.1" 200 38 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:05 -0700] "GET /welcome.html HTTP/1.1" 403 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:11 -0700] "GET /welcome.html HTTP/1.1" 200 1881 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:11 -0700] "GET /style.css HTTP/1.1" 403 0 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:11 -0700] "GET /session/status HTTP/1.1" 200 38 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:24 -0700] "GET /welcome.html HTTP/1.1" 403 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:26 -0700] "GET /welcome.html HTTP/1.1" 200 1881 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:26 -0700] "GET /style.css HTTP/1.1" 403 0 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"
127.0.0.1 - - [01/Jun/2022:22:10:27 -0700] "GET /session/status HTTP/1.1" 200 38 "https://my-service.foo.com/welcome.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0"

【问题讨论】:

  • 我相信我遇到了 NGINX 的一个特殊问题。当我使用https://my-service.foo.com/style.css 时,它总是成功,失败,然后成功,失败。

标签: html css macos nginx firefox


【解决方案1】:

最后,我通过将端口从 7000 更改为 other 解决了这个问题。

在 Mac 上,也要监听端口 7000。 更新端口后,奇怪的行为消失了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 2021-02-24
    • 2012-08-24
    • 2020-02-22
    • 1970-01-01
    • 2023-04-05
    • 2012-01-04
    相关资源
    最近更新 更多