nginx forward https请求到http后端服务器和返回

发布于 2025-02-07 21:37:15 字数 1740 浏览 3 评论 0原文

在单个服务器实例上,我有一个NGINX Web服务器,该服务器在HTTPS上没有任何问题,并且在端口8080上运行的Spring Boot中有一个后端服务器。我不想将此端口打开到Internet,因此我想要要设置具有NGINX的反向代理,以将以/api 开头的请求转发到我的后端并返回响应。

当我将请求发送到浏览器中的域时,在浏览器中运行的前端应用程序会向我的后端发送一些请求(从 /api开始),​​我的前端使用以下基本URL:

http://my-ip:8080/api

NGINX配置如下:我保留:

server {
  listen 80;
  ssl_certificate     /cert/cert.pem;
  ssl_certificate_key /cert/privkey.pem;
  server_name www.mydomain.com;
  rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    listen 443 ssl;
    server_name www.mydomain.com mydomain.com;

    ssl_certificate     /cert/cert.pem;
    ssl_certificate_key /cert/privkey.pem;

    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }
    error_page 404 /index.html;
    location = / {
      root /usr/share/nginx/html;
      internal;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
    location /api {
      proxy_set_header X-Forwarded-Host $host:$server_port;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://127.0.0.1:8080;
    }
}

我保留获取混合内容错误,我的后端请求被阻止浏览器以来我的前端使用HTTP来进行请求。

如果我尝试在前端URL中使用HTTP,例如: https:// my-ip:8080/api

,然后我会遇到不同的错误:

GET https://my-ip/api/... net::ERR_CERT_COMMON_NAME_INVALID

这可能是因为我的证书是为我的域名生成的,而不是为IP生成。

On a single server instance, I have an NGINX web server that operates without any problems with the HTTPS and I have a backend server in Spring Boot running on port 8080. I do not want to open this port to the internet, therefore I would like to setup a reverse proxy with NGINX to forward the request that start with /api to my backend and return the response.

When I send request to the domain in the browser, my frontend application which runs in browser, sends some requests to my backend (starting with /api), my frontend uses the following base url:

http://my-ip:8080/api

And the nginx configuration is as follows:

server {
  listen 80;
  ssl_certificate     /cert/cert.pem;
  ssl_certificate_key /cert/privkey.pem;
  server_name www.mydomain.com;
  rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    listen 443 ssl;
    server_name www.mydomain.com mydomain.com;

    ssl_certificate     /cert/cert.pem;
    ssl_certificate_key /cert/privkey.pem;

    location / {
      root   /usr/share/nginx/html;
      index  index.html index.htm;
      try_files $uri $uri/ /index.html;
    }
    error_page 404 /index.html;
    location = / {
      root /usr/share/nginx/html;
      internal;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
    location /api {
      proxy_set_header X-Forwarded-Host $host:$server_port;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://127.0.0.1:8080;
    }
}

I keep getting Mixed Content Error and my backend requests are being blocked by the browser since my Frontend uses http for the request.

If I try to use https in the Frontend URL, such as:
https://my-ip:8080/api

Then I get a different error:

GET https://my-ip/api/... net::ERR_CERT_COMMON_NAME_INVALID

This is probably because my certificate is generated for my domain name and not for the IP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我爱人 2025-02-14 21:37:15

解决方案:在前端,我们应该将请求发送到HTTPS版本并使用实际域而不是IP地址,因为域名应与认证的域匹配。

请求:https:// my-domain:8080/api

,然后nginx正确转发了此请求。

Solution: In the frontend, we should send the request to https version and use the actual domain instead of the ip address because the domain name should match the domain of the certification.

The request: https://my-domain:8080/api

Then the nginx forwards this request properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文