nginx 反向代理的基本配置以及ssl證書的配置實現https 訪問
1.安裝nginx
pkill -9 apache2 # 關閉apache
sudo apt install nginx # 安裝nginx,使用Ubuntu的包管理工具apt-get
2.修改apache 端口號,防止出錯
sudo vim /etc/apache2/ports.conf # 講端口修改81,防止和nginx 重複,導致重啓失敗
sudo service apache2 start # 啓動apache 服務器,代理目標服務器
3.在nginx 目錄下增加服務器配置
cd /etc/nginx/conf.d/ # 進入配置文件目錄
vim host.conf # 創建自定義配置文件
在conf.d目錄下新安裝的nginx不存在任何文件,新建host.conf文件。在nginx目錄下的nginx.conf中導入了conf.d下所有的文件.如下圖:
所以直接在conf.d下簡歷host配置文件
寫入host.conf 配置文件
server
{
listen 80;
server_name live.triste.com;
index index.html index.htm index.php
root /var/www/html;
location / {
proxy_pass http://localhost:81;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
server
{
listen 80;
server_name info.triste.com;
index index.html index.htm index.php
root /var/www/html;
location / {
proxy_pass http://localhost:81;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
重啓nginx
sudo service nginx reload
效果圖
nginx 訪問
反向代理到apache
反向代理到apache
如何配置SSL 到nginx 並且反向代理呢
假設在擁有了ssl證書的情況下:
直接上代碼如下:
server {
listen 443;
server_name www.domain.com; #填寫綁定證書的域名
ssl on;
ssl_certificate 1_www.domain.com_bundle.crt;
ssl_certificate_key 2_www.domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
ssl_prefer_server_ciphers on;
location / {
root html; #站點目錄
index index.html index.htm;
}
}
上面代碼就配置了www.domain.com
, 現在你就可以通過https://www.domain.com
來訪問了.
配置反向代理:
server {
listen 443;
server_name blog.domain.com; #填寫綁定證書的域名
ssl on;
ssl_certificate blog.domain.com_bundle.crt;
ssl_certificate_key blog.domain.com.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #按照這個協議配置
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;#按照這個套件配置
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:81;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
如上就配置了https 反向代理。
由於國內免費的ssl證書都是單域名證書因此每次配置時候都是需要重新制定證書。
全站加密
編輯nginx.conf 文件,在Ubuntu上的位置在於: /etc/nginx/nginx.conf
http {
# 沈略部分
server {
rewrite ^(.*) https://$host$1 permanent;
}
}
上面代碼即可進行從http 自動跳轉到https 上,從而實現全站加密。