概述:nginx配置https,http共存的步驟大概如下
1.系統(tǒng)需要安裝openssl
2.nginx開啟ssl
3.生成證書密鑰文件
4.nginx配置https ,http
1.安裝openssl (已經(jīng)安裝了請(qǐng)忽略)
wget http://www.openssl.org/source/openssl-1.0.2f.tar.gz
tar -xzf openssl-1.0.2f.tar.gz
cd openssl-1.0.2f
./config --prefix=/usr/local/openssl
make && make install
檢查是否安裝成功
which openssl
2.檢測(cè)nginx 是否安裝了with-http_stub_status_module,with-http_ssl_module
/usr/bin/nginx -V #具體的nginx安裝路徑根據(jù)你的環(huán)境而定
3.安裝nginx SSL模塊 需要從新編譯nginx
cd /usr/local/src/nginx-1.12.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
4.生成證書密鑰文件(存放目錄nginx/conf/ssl)
4.1 創(chuàng)建服務(wù)器證書密鑰文件 server.key
mkdir /usr/local/nginx/conf/ssl
cd /usr/local/nginx/conf/ssl
openssl genrsa -des3 -out server.key 2048
4.2 創(chuàng)建服務(wù)器證書的申請(qǐng)文件 server.csr
openssl req -new -key server.key -out server.csr
輸出內(nèi)容為:
Enter pass phrase for root.key: ← 輸入前面創(chuàng)建的密碼
Country Name (2 letter code) [AU]:CN ← 國家代號(hào)外厂,中國輸入CN
State or Province Name (full name) [Some-State]:BeiJing ← 省的全名姓言,拼音
Locality Name (eg, city) []:BeiJing ← 市的全名,拼音
Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名
Organizational Unit Name (eg, section) []: ← 可以不輸入
Common Name (eg, YOUR name) []: ← 此時(shí)不輸入
Email Address []:admin@mycompany.com ← 電子郵箱商虐,可隨意填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: ← 可以不輸入
An optional company name []: ← 可以不輸入
4.3 備份一份服務(wù)器密鑰文件
cp server.key server.key.org
4.4 去除文件口令
openssl rsa -in server.key.org -out server.key
4.5 生成證書文件server.crt
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
5. 配置nginx 虛擬目錄
https.proxy.test.com.conf
server
{
listen 443 default ssl;
#listen [::]:80 default_server ipv6only=on;
ssl on;
##證書(公鑰.發(fā)送到客戶端的)
ssl_certificate ssl/server.crt;
#私鑰,
ssl_certificate_key ssl/server.key;
ssl_protocols SSLv2 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
server_name proxy.test.com;
index index.html index.htm index.php;
root /home/wwwroot/proxy.test.com;
include enable-php.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
}
6 .配置http重定向到https
http.proxy.test.com.conf
server
{
listen 80 ;
server_name proxy.test.com;
#index index.html index.htm index.php;
#root /home/wwwroot/proxy.test.com;
#error_page 404 /404.html;
include enable-php.conf;
location /nginx_status
{
stub_status on;
access_log off;
}
#重定向到https
if ($scheme = 'http') {
rewrite ^(.*)$ https://$host$uri;
}
#或
# return 301 https://$server_name$request_uri;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
deny all;
}
}
打開瀏覽器 輸入http.proxy.test.com
打開瀏覽器 輸入https.proxy.test.com
如果兩個(gè)地址都指向同一個(gè)地方就OK啦