【问题标题】:Nginx reverse proxy for requesting HTTP backend on HTTPS frontend用于在 HTTPS 前端请求 HTTP 后端的 Nginx 反向代理
【发布时间】:2020-04-06 12:42:26
【问题描述】:

我已经看到了大量关于反向代理和 nginx 的信息,但我对如何实施有点迷茫。我正在运行两个单独的 EC2 实例(前端和后端,后端运行 pm2)。我使用 LetsEncrypt 在前端建立了 SSL,由于混合内容,它不允许我访问我的后端。我该怎么办?

nginx.conf

server {
   listen 80 default_server;
   listen [::]:80 default_server;
   server_name domain;

   location / {}

   return 301 https://$host$request_uri;
}

server {
  listen       443 ssl;
  listen       [::]:443 ssl;
  server_name  localhost;
  root         /insert/root/here;

  ssl_certificate "/path/to/cert";
  ssl_certificate_key "/path/to/key";

  ssl_session_cache shared:SSL:1m;
  ssl_session_timeout  10m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
 ssl_prefer_server_ciphers on;

 include /etc/nginx/default.d/*.conf;

 location / {
 }

 error_page 404 /404.html;
     location = /40x.html {
 }

 error_page 500 502 503 504 /50x.html;
     location = /50x.html {
 }
 }

【问题讨论】:

    标签: ssl nginx amazon-ec2 https reverse-proxy


    【解决方案1】:

    浏览器似乎在抱怨您的 html 内容,因为它具有对外部资源(如 javascript、字体等)的硬编码“http://”引用。 这并不意味着由于这个“混合内容”问题而无法到达后端。

    我在您的配置中没有看到 proxy_pass(或 fastcgi_pass)指令(它应该将请求传递给您的上游后端服务器),所以这可能是您无法访问后端的真正原因。

    您的配置应如下所示:

    server {
        listen 443 ssl;
    
        root /here/are/your/static/files/; # here you can place static html, css, js etc files from your backend to offload backend from serving static files - nginx will take care of them.
        ...
        location / {
            #this means that nginx will forward requests to backend server in case request does not match local static file.
            try_files $uri $uri/ @backend;
        }
    
        location @backend {
            #....
            proxy_pass http://backend-server-ip-address:backend-port
        }
    
    }
    

    【讨论】:

    • 所以我也应该在我的后端实例上运行 nginx 吗?或者这是前端的配置?
    • 好吧。前端和后端术语的使用有时会令人困惑。有些人使用前端术语来表示您的服务的公共接口(可能是由客户端 JS、html 和服务器端组成的商店),而他们的后端是服务的管理接口(它又包含客户端代码和服务器端代码)。其他人将客户端 js/html 称为前端,将服务器端代码称为后端。在您的情况下,我对后端的解释是上游服务器,它通常进行服务器端处理(对于公共 UI 界面和私有 UI 界面)。
    • @rlax13,如果您有两个独立的服务器来提供公共功能(供访客)和私人功能(供员工),请将此信息添加到您的问题中。
    猜你喜欢
    • 1970-01-01
    • 2021-03-02
    • 2021-11-09
    • 2018-02-22
    • 2016-06-14
    • 2021-10-13
    • 2012-10-25
    • 2012-11-04
    • 2017-05-16
    相关资源
    最近更新 更多