新手问题 Nginx 配置问题 (想要 https 和 http 都可以访问)

starshine · 发布于 2016年12月22日 · 最后由 jiyuhang110 回复于 2016年12月28日 · 1097 次阅读
96

我的最近也在玩这个,但是总是不生效,希望大神指教,我的配置如下:(我这样配置是有问题的,希望大神们指正)

server {

listen 24766;

listen 443 ssl;

ssl_certificate /opt/zs/33iq.crt;

ssl_certificate_key /opt/zs/33iq.key;

root /var/www/star_p/current/;

location / {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_pass http://star_p;

} }

server {

listen 24766;

server_name demo.star.net;

rewrite ^ https://$server_name$request_uri permanent;

}
共收到 11 条回复
17863

参考: https://github.com/80percent/rails-template/blob/master/files/config/nginx.conf.example ,这是个http的配置, https的自己再搜一下吧, 很简单。 拿去不谢

96

@rina 我这是想要实现 https 和 http 都可以访问的情况,http 我可以配置

De6df3

说清楚,你的问题是什么!!!

整理好你贴上来的 Nginx 配置信息

160

分别:

listen 443 ssl;

listen 80;

但是你的 http里面是跳转成https。

还有 根据你ssl证书的要求修改一下支持的协议和加密算法

ssl on;
ssl_certificate /opt/zs/33iq.crt;
ssl_certificate_key /opt/zs/33iq.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:HIGH:!aNULL:!MD5:!ADH:!DH;
ssl_prefer_server_ciphers on;

你配置文件贴的不全吧。

17025

楼主参考这个网页的设置,关键点是 proxy_set_header X-Forwarded-Proto https; https://www.digitalocean.com/community/questions/error-too-many-redirect-on-nginx

I just had the exact same problem after enabling SSL. The issue for me was I have Rails set to config.force_ssl = true which relies on the proxy_set_header X-Forwarded-Proto https; header to know whether the request was over SSL or not. I didn't have that header to Rails would receive an SSL request thinking it wasn't with SSL which caused it to redirect to https.

Here's my working config:

upstream puma {
  server unix:///home/deploy/apps/genus/shared/tmp/sockets/genus-puma.sock;
}

server {
  listen 80;
  listen 443 ssl;
  server_name genusapp.com;

  ssl_certificate /home/deploy/apps/genus/genusapp_com.pem;
  ssl_certificate_key /home/deploy/apps/genus/genusapp_com.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;


  root /home/deploy/apps/genus/current/public;
  access_log /home/deploy/apps/genus/current/log/nginx.access.log;
  error_log /home/deploy/apps/genus/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}
96

@jiyuhang110 我没有使用 80 默认端口,而是适用的是 其他的端口 监听

96

@sandy_xu 谢谢 回复,我的配置如果不使用 默认 80端口,而是使用 其他的端口进行监听,怎么办?

17025

#9楼 @starshine 非80端口不清楚。不过我知道你的配置肯定缺少proxy_set_header X-Forwarded-Proto https,楼主多尝试,成功后求分享经验

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册