使用NGINX代理HTTPS请求到HTTP后端


30

我已将nginx配置为我的外部可见的Web服务器,该服务器通过HTTP与后端通信。

我要实现的方案是:

  1. 客户端向nginx发出HTTP请求,该请求重定向到同一URL,但通过HTTPS
  2. Nginx代理通过HTTP请求到后端
  3. nginx通过HTTP从后端接收响应。
  4. Nginx通过HTTPS将其传递回客户端

我当前的配置(正确配置了后端)是:

服务器{
        听80;
        server_name localhost;

        位置〜。* {
            proxy_pass http:// backend;
            proxy_redirect http://后端https:// $ host;
            proxy_set_header主机$ host;
            }
        }

我的问题是通过HTTP而非HTTPS发送对客户端的响应(第4步)。有任何想法吗?

Answers:


6

您尝试设置的代理类型称为反向代理。快速搜索反向代理Nginx,使我获得此页面:

http://intranation.com/entries/2008/09/using-nginx-reverse-proxy/

除了添加一些有用的功能(例如X-Forwarded-For标头)(可让您的应用查看实际的源IP)之外,它还专门执行以下操作:

proxy_redirect off

祝好运!:)


1
感谢您的回复-该链接确实非常有用。我想我已经解决了我的问题,方法是将其拆分-我将端口80上的所有HTTP请求重定向到端口443上的HTTPS。然后我在config中为443配置了新服务器。一样的地方。
麦克,2010年

35

我在生产中使用以下配置

server {
    listen xxx.xxx.xxx.xxx:80;
    server_name www.example.net;

    rewrite ^(.*) https://$server_name$1 permanent;
}

server {
    listen xxx.xxx.xxx.xxx:443;
    server_name www.example.net;

    root   /vhosts/www.example.net;

    ssl                  on;
    ssl_certificate      /etc/pki/nginx/www.example.net.crt;
    ssl_certificate_key  /etc/pki/nginx/www.example.net.key;

    ssl_prefer_server_ciphers on;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # Diffie-Hellman parameter for DHE ciphersuites, recommended 2048 bits
    ssl_dhparam /etc/pki/nginx/dh2048.pem;

    # intermediate configuration. tweak to your needs.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
    }
}

+1添加proxy_set_header X-Forwarded-Proto https;帮了我大忙。
Faisal Feroz

-3
server {

    listen 80;
    server_name www.example.net example.net;

   rewrite ^/(.*)$ https://$host$request_uri? permanent; 
}

server {
  listen 443;
  server_name www.example.net example.net;
  .....................
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.