Nginx安裝及其相關配置
- 一、Nginx安裝
- 1. 去官網http://nginx.org/下載對應的nginx包,推薦使用穩定版本
- 2. 上傳nginx到linux系統
- 3. 安裝依賴環境
- 4.解壓,需要注意,解壓后得到的是原始碼,原始碼需要編譯后才能安裝
- 5.編譯之前,先創建nginx臨時目錄,如果不創建,在啟動nginx的程序中會報錯
- 6.在nginx目錄,輸入如下命令進行配置,目的是為了創建makefile檔案
- 7.make編譯&安裝
- 8.進入sbin目錄啟動nginx
- 二、配置反向代理
- 1、配置upstream
- 2、配置server
- 三、配置負載均衡
- nginx默認采用輪訓的方式進行負載均衡
- 四、upstream指令引數
- 1、keepalived
- 2、控制瀏覽器快取
- 3、反向代理快取
- 五、配置ssl證書提供https訪問
- 1. 安裝SSL模塊
- 2、配置HTTPS
一、Nginx安裝
1. 去官網http://nginx.org/下載對應的nginx包,推薦使用穩定版本
2. 上傳nginx到linux系統
3. 安裝依賴環境
(1)安裝gcc環境
yum install gcc-c++
(2)安裝PCRE庫,用于決議正則運算式
yum install -y pcre pcre-devel
(3)zlib壓縮和解壓縮依賴
yum install -y zlib zlib-devel
(4)SSL 安全的加密的套接字協議層,用于HTTP安全傳輸,也就是https
yum install -y openssl openssl-devel
4.解壓,需要注意,解壓后得到的是原始碼,原始碼需要編譯后才能安裝
tar -zxvf nginx-1.16.1.tar.gz
5.編譯之前,先創建nginx臨時目錄,如果不創建,在啟動nginx的程序中會報錯
mkdir /var/temp/nginx -p
6.在nginx目錄,輸入如下命令進行配置,目的是為了創建makefile檔案
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:\ 代表在命令列中換行,用于提高可讀性配置命令:

7.make編譯&安裝
make && make install
8.進入sbin目錄啟動nginx
啟動:nginx
停止:./nginx -s stop
重新加載:./nginx -s reload
二、配置反向代理
1、配置upstream
upstream [proxyName] {
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
2、配置server
server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
}
}
三、配置負載均衡
nginx默認采用輪訓的方式進行負載均衡
1、使用加權輪詢 weight指定權重
upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}
2、hash負載均衡
upstream [proxyName] {
ip_hash
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
hash演算法實際上只會計算 192.168.1這段做哈希
使用ip_hash的注意點:
不能把后臺服務器直接移除,只能標記down.
3、url hash負載均衡
upstream [proxyName] {
hash $request_url;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
4、最小連接負載均衡
upstream [proxyName] {
least_conn;
server 192.168.1.173:8080;
server 192.168.1.174:8080;
server 192.168.1.175:8080;
}
四、upstream指令引數
· max_conns:限制最大同時連接數 1.11.5之前只能用于商業版
· slow_start:單位秒,權重在指定時間內從1上升到指定值,不適用與hash負載均衡、隨機負載均衡 如果在 upstream 中只有一臺 server,則該引數失效(商業版才有)
· down:禁止訪問
· backup:備用機 只有在其他服務器無法訪問的時候才能訪問到 不適用與hash負載均衡、隨機負載均衡
· max_fails:表示失敗幾次,則標記server已宕機,剔出上游服務 默認值1
· fail_timeout:表示失敗的重試時間 默認值10
1、keepalived
upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
keepalive 32; #保持的連接數
}
server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
proxy_http_version 1.1; #連接的協議版本
proxy_set_header Connection ""; 清空連接請求頭
}
}
2、控制瀏覽器快取
server {
listem 80;
server_name www.tomcats.com;
location / {
proxy_pass http://tomcats;
expires 10s; #瀏覽器快取10秒鐘
#expires @22h30m #在晚上10點30的時候過期
#expires -1h #快取在一小時前時效
#expires epoch #不設定快取
#expires off #快取關閉,瀏覽器自己控制快取
#expires max #最大過期時間
}
}
3、反向代理快取
upstream [proxyName] {
server 192.168.1.173:8080 weight=1;
server 192.168.1.174:8080 weight=5;
server 192.168.1.175:8080 weight=2;
}
#proxy_cache_path 設定快取保存的目錄的位置
#keys_zone設定共享內以及占用的空間大小
#mas_size 設定快取最大空間
#inactive 快取過期時間,錯過此時間自動清理
#use_temp_path 關閉零時目錄
proxy_cache_path /usr/local/nginx/upsteam_cache keys_zone=mycache:5m max_size=1g inactive=8h use_temp_path=off;
server {
listem 80;
server_name www.tomcats.com;
#開啟并使用快取
proxy_cache mycache;
#針對200和304回應碼的快取過期時間
proxy_cache_valid 200 304 8h;
location / {
proxy_pass http://tomcats;
}
}
五、配置ssl證書提供https訪問
1. 安裝SSL模塊
要在nginx中配置https,就必須安裝ssl模塊,也就是: http_ssl_module,
進入到nginx的解壓目錄:/home/software/nginx-1.16.1
新增ssl模塊(原來的那些模塊需要保留)
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi \
--with-http_ssl_module
編譯和安裝
make && make install
2、配置HTTPS
把ssl證書 *.crt 和 私鑰 *.key 拷貝到/usr/local/nginx/conf目錄中,
新增 server 監聽 443 埠:
server {
listen 443;
server_name www.imoocdsp.com;
# 開啟ssl
ssl on;
# 配置ssl證書
ssl_certificate 1_www.imoocdsp.com_bundle.crt;
# 配置證書秘鑰
ssl_certificate_key 2_www.imoocdsp.com.key;
# ssl會話cache
ssl_session_cache shared:SSL:1m;
# ssl會話超時時間
ssl_session_timeout 5m;
# 配置加密套件,寫法遵循 openssl 標準
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://tomcats/;
index index.html index.htm;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/413867.html
標籤:其他
下一篇:第 6 章 計算機網路
