一豆村、安裝部署Nginx
1、安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel
openssl openssl-devel
2猩谊、下載安裝Nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar -zxvf nginx-1.8.0.tar.gz
./configure
make&&make install
3以蕴、啟動Nginx
- Nginx默認(rèn)安裝路路徑
/usr/local/nginx
- 訪問配置
防?火墻開放端口糙麦,阿里云網(wǎng)絡(luò)安全組配置80端口
firewall-cmd --permanent --zone=public --add-port=80/tcp #開放指定端口
image.png
cd /usr/local/nginx #進(jìn)入nginx根目錄
sudo ./sbin/nginx #啟動nginx,nginx啟動目錄在sbin文件目錄下
-
輸入訪問地址,Nginx啟動部署成功
image.png
4.Nginx基本操作命令
./nginx #默認(rèn)配置?文件啟動
./nginx -s reload #重啟丛肮,加載默認(rèn)配置?文件
./nginx -s stop # 快速停止
./nginx -s quit # 正常停止
./nginx -c /usr/local/nginx/conf/nginx.conf
#啟動指定某個配置?文件
./nginx -s stop #停?止
#關(guān)閉進(jìn)程赡磅,nginx有master process 和worker,process,關(guān)閉master即可
ps -ef | grep nginx
kill -9 PID
二、使用Nignx搭建圖片-文件服務(wù)器
-
配置nginx.conf
server {
listen 80;
server_name aabbccdd.com;
location /app/img {
alias /usr/local/software/img/;
}
}
-
注意
- 在location / 中配置root目錄
- 在location /path中配置alias虛擬目錄宝与, 目錄后面的"/"符號一定要帶上
三焚廊、Nginx配置集群應(yīng)用-負(fù)載均衡策略
1、Nginx負(fù)載均衡節(jié)點輪詢(默認(rèn))
upstream lbs {
server 192.168.0.202:8080;
server 192.168.0.202:8081;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
2习劫、Nginx負(fù)載均衡ip_hash(固定分發(fā))
upstream lbs {
ip_hash;
server 192.168.0.202:8080;
server 192.168.0.202:8081;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
3咆瘟、Nginx負(fù)載均衡weight 權(quán)重配置
- 簡介:weight和訪問比率成正比,數(shù)字越大诽里,分配得到的流量越高
- 場景:服務(wù)器?性能差異大的情況使用
upstream lbs {
server 192.168.0.202:8080 weight=5;
server 192.168.0.202:8081 weight=10;
}
location /api/ {
proxy_pass http://lbs;
proxy_redirect default;
}
4搞疗、Nginx探測后端節(jié)點可用性和配置
- 參數(shù)解釋
- max_fails=N 設(shè)定Nginx與后端節(jié)點通信的嘗試失敗的次數(shù)。
- 在fail_timeout參數(shù)定義的時間內(nèi),如果失敗的次數(shù)達(dá)到此值匿乃,Nginx就這個節(jié)點不可用桩皿。
- 在下?個fail_timeout時間段到來前,服務(wù)器?不不會再被嘗試幢炸。
- 失敗的嘗試次數(shù)默認(rèn)是1泄隔,如果設(shè)為0就會停止統(tǒng)計嘗試次數(shù),認(rèn)為服務(wù)器?是?直可用的宛徊。
upstream lbs {
server 192.168.0.202:8080 max_fails=2 fail_timeout=60s ;
server 192.168.0.202:8081 max_fails=2 fail_timeout=60s;
}
location /api/ {
proxy_pass http://lbs;
proxy_next_upstream error timeout http_500 http_503 http_404;
}
四佛嬉、 Nginx-全局異常兜底數(shù)據(jù)返回
- 將404,500等錯誤的狀態(tài)碼定向?200,返回了全局兜底數(shù)據(jù)
location / {
proxy_pass http://lbs;
proxy_redirect default;
# 存放?戶的真實ip
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 http_503 non_idempotent;
#開啟錯誤攔截配置,?定要開啟
proxy_intercept_errors on;
}
# 不加 =200闸天,則返回的就是原先的http錯誤碼暖呕;配上后如果出現(xiàn)500等錯誤都返回給?戶200狀態(tài),并跳轉(zhuǎn)?/default_api
error_page 404 500 502 503 504 =200;
location = /default_api {
default_type application/json;
return 200 '{"code":"-1","msg":"invokefail, not found "}';
}
五苞氮、 Nginx-封禁惡意IP
- 所有?站屏蔽IP的?法湾揽,把include xxx; 放到http{}語句塊。
http{
# ....
include blacklist.conf;
}
- blacklist.conf?錄下?件內(nèi)容
deny 192.168.0.201;
deny 192.168.0.133;
六笼吟、 Nginx配置解決瀏覽器跨域
location / {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header Access-Control-Allow-Methods 'GET,POST,OPTIONS';
#如果預(yù)檢請求則返回成功,不需要轉(zhuǎn)發(fā)到后端
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 200;
}
}
七库物、地址重定向-Nginx的rewrite規(guī)則應(yīng)?
location / {
rewrite ^/(.*) www.baidu.com/$1 permanent
proxy_pass http://lbs;
proxy_redirect default;
}
八、Nginx配置Websocket反向代理
location / {
proxy_pass http://lbs;
proxy_read_timeout 300s; //websocket #空閑保持時?
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_http_version 1.1;
#核?是下?的配置 其他和普通反向代理沒區(qū)別, 表示請求服務(wù)器升級協(xié)議為WebSocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection
$connection_upgrade
}
九贷帮、Nginx的配置服務(wù)端緩存
http {
proxy_cache_path /root/cache levels=1:2
keys_zone=xd_cache:10m max_size=1g
inactive=60m use_temp_path=off;
server {
location /{
...
proxy_cache xd_cache;
proxy_cache_valid 200 304 10m;
proxy_cache_valid 404 1m;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache "$upstream_cache_status";
}
}
}
- 清空緩存直接rm /root/cache(設(shè)置緩存文件路徑)下的對應(yīng)文件
- 可以nginx?志模板增加信息 $upstream_cache_status 查看日志模板上的對應(yīng)信息
十戚揭、 Nginx性能優(yōu)化之靜態(tài)資源壓縮
#開啟gzip,減少我們發(fā)送的數(shù)據(jù)量
http {
gzip on;
gzip_min_length 1k;
#4個單位為16k的內(nèi)存作為壓縮結(jié)果流緩存
gzip_buffers 4 16k;
#gzip壓縮?,可在1~9中設(shè)置撵枢,1壓縮?最?民晒,速度最快,9壓縮?最?锄禽,速度最慢潜必,消耗CPU
gzip_comp_level 4;
#壓縮的類型
gzip_types application/javascript text/plain
text/css application/json application/xml
text/javascript;
#給代理服務(wù)器?的,有的瀏覽器?持壓縮沟绪,有的不?持刮便,所以避免浪費(fèi)不?持的也壓縮空猜,所以根據(jù)客戶的HTTP頭來判斷绽慈,是否需要壓縮
gzip_vary on;
#禁?IE6以下的gzip壓縮,IE某些版本對gzip的壓縮?持很不好
gzip_disable "MSIE [1-6].";
}
- 從文件服務(wù)器下載文件會發(fā)現(xiàn)文件會被壓縮
location /static {
alias /usr/local/software/static;
}
十一辈毯、 Nginx配置https證書配置
1.重新安裝nginx坝疼,加入ssl模塊
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make&&make install
#查看是否成功
/usr/local/nginx/sbin/nginx -V
- Nginx配置https證書
server {
listen 443 ssl;
server_name 16web.net;
ssl_certificate cert/cert.pem; #證書的pem路徑
ssl_certificate_key cert/cert.key; #證書的key路徑
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
- 訪問配置
防?火墻開放端口,阿里云網(wǎng)絡(luò)安全組配置443端口