配置https首先要有ssl證書,這個證書目前阿里有免費的,但如果自己做實驗,也是可以自簽證書,只不過不受信
openssl genrsa -des3 -out server.key 1024 ##創建服務器私鑰
openssl req -new -key server.key -out server.csr ##創建簽名請求的證書
cp server.key server.key.org ##復制一份
openssl rsa -in server.key.org -out server.key ##
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
此時你的目錄會有 server.key server.crt 兩個文件。之后,配置nginx
server {
listen 80 ; ##這一步 是為了后面的http轉換https ,如果不需要 可以不寫。
listen 443;
server_name www.c.com;
if ($scheme = http) { ##這一步是為了把http的請求重定向為https,如果不需要 可以不寫
return 301 https://$host$request_uri;
}
charset utf-8;
ssl on;
ssl_certificate /usr/local/nginx/server.crt; ##生成的crt文件
ssl_certificate_key /usr/local/nginx/server.key; ## 生成的key文件
ssl_session_timeout 5m; ##session超時時間
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ## 這個和底下的都是官網給的默認的照着寫就行
ssl_ciphers HIGH:!aNULL:!MD5;
root /local/nginx/;
access_log /local/nginx/logs/PosWeb/https_access.log main;
include /etc/nginx/default.d/*.conf;
include /etc/nginx/utils/honeyjar/keywords.conf;
location / {
proxy_set_header X-FORWARDED-PROTO https;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
proxy_pass https://upstream;
}
然后基本OK。自己去網上找個受信的證書。基本就可以用來了
如何處理一個請求
首先看一個nginx部分的配置文件:
upstream PosWeb {
server pos-web01:8081;
server pos-web02:8081;
}
server {
listen 80;
server_name
www.a.com
www.b.com;
charset utf-8;
root /local/nginx/;
access_log /local/nginx/logs/PosWeb/access.log main;
include /etc/nginx/utils/honeyjar/keywords.conf;
location / {
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
proxy_pass http://PosWeb;
}
}
upstream PosWeb_https {
server pos-web01:9143;
server pos-web02:9143;
}
server {
listen 443;
server_name www.c.com;
charset utf-8;
ssl on;
ssl_certificate /usr/local/nginx/ssl/fcwangpu.crt;
ssl_certificate_key /usr/local/nginx/ssl/fcwangpu.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /local/nginx/;
access_log /local/nginx/logs/PosWeb/https_access.log main;
include /etc/nginx/default.d/*.conf;
include /etc/nginx/utils/honeyjar/keywords.conf;
location / {
proxy_set_header X-FORWARDED-PROTO https;
proxy_set_header Host ;
proxy_set_header X-Real-IP ;
proxy_set_header X-Forwarded-For ;
proxy_pass https://PosWeb_https;
}
}
}
以上配置,一個443端口的https 一個是80端口的http
但是有個問題,比如我訪問https://www.a.com 是可以訪問的。
疑惑是我並沒有給www.a.com這個域名配置https,為什么https可以訪問www.a.com
后來去官網nginx.org查看
nginx請求是這樣,在官網原文是這樣寫的
In this configuration nginx tests only the request’s header field “Host” to determine which server the request should be routed to. If its value does not match any server name, or the request does not contain this header field at all, then nginx will route the request to the default server for this port. In the configuration above, the default server is the first one — which is nginx’s standard default behaviour. It can also be set explicitly which server should be default, with the default_server parameter in the listen directive
以上意思翻譯過來是說,請求會根據你的host進行匹配,如果找到匹配項,就去匹配的項目上請求。如果沒有找到匹配項,就會去這個端口默認的服務請求,也就是默認的serve_name,默認的server_name 怎么判定,默認的是寫在這個端口上的第一個server,就默認為他是默認服務。
看一個配置文件
server{
listen 80;
server_name bbb.org www.bbb.org;
}
server{
listen 80;
server_name aaa.org www.aaa.org;
}
以上配置,默認 bbb.org 為默認的配置,也就是說,我用http請求 ,對這台服務器,請求了一個www.ccc.com 但是80端口並沒有這個server_name ,那么這個請求就
會自動跑到第一個上面,也就是說請求會到bbb.org上。
(以下也是nginx.org 官網的原話 這個意思和上面的差不太多,感興趣自己翻譯一下)
The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair. If none of the directives have the default_server parameter then the first server with the address:port pair will be the default server for this pair
那么,如何配置我們的aaa.org為默認端口,以下配置就可以
server{
listen 80;
server_name bbb.org www.bbb.org;
}
server{
listen 80 default_server;
server_name aaa.org www.aaa.org;
}
這樣,如果我去請求 www.ccc.com 請求找不到,就會自動去aaa.org上面去請求。