设置SSL – https工作,http不工作

更新:我find的临时解决scheme是只redirect到https的所有http请求。

我一直在努力在我的网站上设置SSL。 这就像在一个Rails上。

我使用nginx与独angular兽。 我从GoDaddy购买了证书。 然后,我按照本文档的说明进行设置并在/etc/nginx/ssl生成CSR。

然后,我复制了生成的.csr文件的文本,并用它从GoDaddy发出证书。 证书颁发后,我下载了在GoDaddy上生成的密钥包,并遵循本文档中的指示。

然后configuration我的/etc/nginx/sites-enabled/mysite.conf文件:

(我在服务器{…}下添加了以下几行

  listen 443; ssl on; ssl_certificate /etc/nginx/ssl/mysite.crt; ssl_certificate_key /etc/nginx/ssl/mysite.key; 

(我更换了listen 80 default; listen 443;

然后,我重新启动服务器,并使HTTPS://www.example.com正常工作。 https是绿色的,显示证书没问题。

但是,当我浏览到HTTP://www.example.com我得到一个错误:502错误的网关 – nginx。

我不确定是什么原因造成的。 任何线索?

如果你需要任何其他信息让我知道,我会张贴他们。

我的网站configuration:

 /etc/nginx/sites-enabled/example.conf upstream example { server unix:/u/app/example/shared/.sock fail_timeout=0; } server { listen 80; server_name www.example.com; root /u/app/example/current/public/; access_log /u/app/example/shared/log/nginx.access.log; error_log /u/app/example/shared/log/nginx.error.log; client_max_body_size 20M; try_files $uri/index.html $uri.html $uri @app; location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://example; } } 

我的/etc/nginx/nginx.conf

 user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 1024; } http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; upstream app_server { server 127.0.0.1:8080 fail_timeout=0; } access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable "msie6"; gzip_types text/plain text/xml text/css text/comma-separated-values; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } 

和/ etc / nginx / sites-available / default文件是:

 server { root /u/app/example/current/public; server_name _; index index.htm index.html; location / { try_files $uri/index.html $uri.html $uri @app; } # location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|$ location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ { try_files $uri @app; } location @app { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_redirect off; proxy_pass http://app_server; } } 

我有两个日志文件:

听80; 听443;

输出212.50.121.69 - - [28/Jul/2014:15:35:53 +0000] "-" 400 0 "-" "-"

你需要使用:

 listen 80; listen 443 ssl; 

在那些你想同时使用https和http的虚拟主机上。

我宁愿从httpredirect到https。 您需要为http创build单独的虚拟主机,将所有请求redirect到https版本。

回答你的问题:

  I replaced listen 80 default; to listen 443; 

不要更换listen 80

相反,添加listen 443

更具体:

来自stackexchange的亚历山大·阿扎罗夫

 The error says it all actually. Your configuration tells Nginx to listen on port 80 (HTTP) and use SSL. When you point your browser to `http://localhost`, it tries to connect via HTTP. Since Nginx expects SSL, it complains with the error. 

解决方法非常简单。 你需要两个服务器部分:

 server { listen 80; // other directives... } server { listen 443; ssl on; // SSL directives... // other directives... 

}