1、http 重定向
[圖片上傳失敗...(image-ca2188-1534050760876)]
2、一臺(tái)主機(jī),同時(shí)提供http與https兩種訪問方式
1. nginx.conf
- 1)域名站點(diǎn)最終通過https訪問
- 2)http首先訪問nginx后,重定向到https訪問(http => nginx => https)
http
{
# 1、https 443端口:https://www.domain.com
server
{
listen 443;
ssl on;
server_name domain.com; //你的域名
index index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
root /home/wwwroot/web/public;//項(xiàng)目根目錄
include laravel.conf;
#error_page 404 /404.html;
include enable-php.conf;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
}
# 2、http 80端口:http://www.domain.com, 重定向到 https 443端口
server
{
listen 80;
server_name domain.com;
## 重定向到 443 https 方式訪問
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}
}
- 1)nginx服務(wù)部署所在主機(jī),直接作為【http服務(wù)器】
- 2)同時(shí)提供【80端口】與【443】端口
- 3)http重定向?yàn)閔ttps
- 4)如果是【自制ssl證書】,則需要在【app客戶端】預(yù)先存放【ssl公鑰證書】
- 5)如果是【權(quán)威認(rèn)證機(jī)構(gòu)頒發(fā)的ssl證書】,則不需要做什么處理
1. 直接使用 http
[圖片上傳失敗...(image-199c6e-1534050760876)]
2. http 轉(zhuǎn)發(fā)為 https
[圖片上傳失敗...(image-287921-1534050760876)]
3、【nginx 轉(zhuǎn)發(fā)服務(wù)】與【nginx http服務(wù)】分離
1. 邏輯流程
[圖片上傳失敗...(image-f5f4f5-1534050760876)]
2. nginx 服務(wù)器
| nginx 服務(wù)器類型 | 服務(wù)器編號(hào) | |
|---|---|---|
| nginx 轉(zhuǎn)發(fā)服務(wù)器 | A | 8080 |
| nginx http web 服務(wù)器 | B |
3.【A主機(jī)】nginx.conf
server
{
listen 8080;
server_name domain.com;
# 1、將對(duì)localhost訪問由/opt/app/lua/dep.lua進(jìn)行處理
location /
{
default_type "text/html";
content_by_lua_file /opt/app/lua/dep.lua; # 指定由lua文件處理http請(qǐng)求
#add_after_body "$http_x_forwarded_for";
}
############################################################
# 2.1、http 協(xié)議進(jìn)行轉(zhuǎn)發(fā)
location @go_http
{
# 訪問nginx【http】服務(wù)器的http協(xié)議時(shí),
# => 使用 proxy_pass 轉(zhuǎn)發(fā)
# => proxy_pass【不會(huì)讓地址欄發(fā)生變化】
proxy_pass http://【nginx http服務(wù)器ip】:80;
}
############################################################
# 2.2、https 協(xié)議轉(zhuǎn)發(fā)進(jìn)行轉(zhuǎn)發(fā)
location @go_https
{
# 訪問nginx【https】服務(wù)器的http協(xié)議時(shí)
# => 使用 rewrite 重定向
# => rewrite 【讓地址欄發(fā)生變化,重新發(fā)起https連接訪問】
# => 如果是【自制ssl證書】,則需要在【app客戶端】預(yù)先存放【ssl公鑰證書】
# => 如果是【權(quán)威認(rèn)證機(jī)構(gòu)頒發(fā)的ssl證書】,則不需要做什么處理
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
4.【A主機(jī)】/opt/app/lua/dep.lua
1. lua 源文件
-- 查詢memcache等數(shù)據(jù)庫服務(wù),
-- 判斷ip是否包含在http或https協(xié)議訪問配置中
-- 走h(yuǎn)ttp協(xié)議
if res == "1" then
ngx.exec("@go_http")
return
end
-- 走h(yuǎn)tts協(xié)議
ngx.exec("@go_https")
2. ngx.exec("@go_https")
location @go_https
{
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}
3. ngx.exec("@go_http")
location @go_http
{
proxy_pass http://【nginx http服務(wù)器ip】:80;
}
會(huì)自動(dòng)重定向?yàn)樯厦娴膆ttps鏈接訪問。
5.【B主機(jī)】nginx.conf
http
{
# 1. 443端口 https://www.domain.com
server
{
listen 443;
ssl on;
server_name localhost; //你的域名
index index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
root /home/wwwroot/web/public;//項(xiàng)目根目錄
}
# 2. 80端口 https://www.domain.com
server
{
listen 80;
server_name localhost;
root /home/wwwroot/web/public;//項(xiàng)目根目錄
}
}
- 1)提供【80端口】的http請(qǐng)求處理
- 2)也提供【443端口】的http請(qǐng)求處理
6. rewrite 與 proxy_pass
| rewrite | 重定向 | 讓客戶端重新發(fā)起連接,訪問新的url地址,地址欄會(huì)發(fā)生變化 |
| proxy_pass | 代理轉(zhuǎn)發(fā) | 直接將客戶端請(qǐng)求塞給另外服務(wù)器處理,地址欄【不發(fā)生】變化 |
4、nginx 497 響應(yīng)碼
1. 核心
- 1、當(dāng)網(wǎng)站只允許https訪問時(shí),當(dāng)用http訪問時(shí)nginx會(huì)報(bào)出497錯(cuò)誤碼
- 2、利用error_page命令,將497狀態(tài)碼的鏈接重定向到https域名
2. nginx.conf
server
{
listen 127.0.0.1:443; #ssl端口
listen 127.0.0.1:80; #用戶習(xí)慣用http訪問,加上80,后面通過497狀態(tài)碼讓它自動(dòng)跳到443端口
server_name dev.wangshibo.com;
#為一個(gè)server{......}開啟ssl支持
ssl on;
#指定PEM格式的證書文件
ssl_certificate /etc/nginx/wangshibo.pem;
#指定PEM格式的私鑰文件
ssl_certificate_key /etc/nginx/wangshibo.key;
#讓http請(qǐng)求重定向到https請(qǐng)求
error_page 497 https://$host$uri?$args;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
5、proxy_redirec
## re-write redirects to http as to https, example: /home
proxy_redirect http:// https://;