Nginx的跨域、alias、優(yōu)化方式
更新時間:2024年08月06日 16:55:55 作者:Forever Nore
這篇文章主要介紹了Nginx的跨域、alias、優(yōu)化方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
root與alias
location / { alias /app/html/; index index.html index.htm; }
兩者區(qū)別:
alias
是目錄別名,root是最上層目錄的定義alias
后必須用"/"結束,不然找不到文件,root可有可無
反向代理解決跨域
- LVS:四層負載均衡,基于tcp ip和端口號 實現(xiàn)負載均衡
- Nginx:七層負載均衡,對http協(xié)議 實現(xiàn)負載均衡
nginx的優(yōu)化
基本配置優(yōu)化
- 查看CPU核數(shù)
cat /proc/cpuinfo| grep "cpu cores"| uniq
worker_processes 4 ; # 設為cpu核數(shù) 啟動的worker進程數(shù) events{ #設置Nginx網絡連接序列化 accept_mutex on; #設置Nginx的worker進程是否可以同時接收多個請求 multi_accept on; #設置Nginx的worker進程最大的連接數(shù) worker_connections 1024; #設置Nginx使用的事件驅動模型 use epoll; } http { include mime.types; #include是引入關鍵字,這里引入了mime.types這個配置文件(同在conf目錄下,mime.types是用來定義,求返回的content-type) default_type application/octet-stream; #mime.types未定義的,使用默認格式application/octet-stream sendfile on; # 開啟 高效文件傳輸模式。 tcp_nopush on; #需要在 sendfile開啟模式才有效,防止網路阻塞,積極的減少網絡報文段的數(shù)量。將響應頭和正文的開始部分一起發(fā)送,而不一個接一個的發(fā)送 tcp_nodelay on; #有數(shù)據(jù)隨時發(fā)送 keepalive_timeout 65; #長鏈接超時時間 #一個nginx可以啟用多個server(虛擬服務器) server { listen 80;#監(jiān)聽端口80 server_name localhost; #接收的域名 location / { root html; #根目錄指向html目錄,看下圖 index index.html index.htm; #域名/index 指向 index.html index.htm文件,看下圖 } error_page 500 502 503 504 /50x.html; # 服務器錯誤碼為500 502 503 504,轉到"域名/50x.html" location = /50x.html { # 指定到html文件夾下找/50x.htm root html;#根目錄指向html目錄,看下圖 } } }
反向代理設置keepalive
- 轉發(fā)請求 提高效率
upstream backend{ server 192.168.111.101:9001; server 192.168.111.101:9002; server 192.168.111.101:9003; keepalive 300; # 300個長連接,轉發(fā)請求效率大大提高! } server { listen 80; server_name localhost; location /{ proxy_pass http://backend; } }
壓縮
server { listen 80; server_name localhost; gzip on; gzip_buffers 32 4K; gzip_comp_level 6; gzip_min_length 100; gzip_types application/javascript text/css text/xml application/json; gzip_disable "MSIE [1-6]\."; #配置禁用gzip條件,支持正則。此處表示ie6及以下不啟用gzip(因為ie低版本不支持) gzip_vary on; #accept-encoding gzip_static on; #如果有壓縮好的,直接使用 location / { proxy_pass http://127.0.0.1:8080; } }
配置文件內容詳細介紹:
gzip配置的常用參數(shù)
gzip on|off
; #是否開啟gzipgzip_buffers
32 4K| 16 8K #緩沖(壓縮在內存中緩沖幾塊? 每塊多大?)gzip_comp_level
[1-9] #推薦6 壓縮級別(級別越高,壓的越小,越浪費CPU計算資源)gzip_disable
#正則匹配UA 什么樣的Uri不進行gzipgzip_min_length
200 # 開始壓縮的最小長度(再小就不要壓縮了,意義不在)gzip_http_version
1.0|1.1 # 開始壓縮的http協(xié)議版本(可以不設置,目前幾乎全是1.1協(xié)議)gzip_proxied
# 設置請求者代理服務器,該如何緩存內容gzip_types
text/plain application/xml # 對哪些類型的文件用壓縮 如txt,xml,html ,cssgzip_vary on|off
# 是否傳輸gzip壓縮標志
緩存
# 代理緩存配置 # meitecache:256m,大小256m,失效時間1天 inactive=1d proxy_cache_path "./meite_cachedata" levels=1:2 keys_zone=meitecache:256m inactive=1d max_size=1000g; server { listen 80; server_name localhost; location /details { #使用緩存名稱 proxy_cache meitecache; #對以下狀態(tài)碼實現(xiàn)緩存~~~~ proxy_cache_valid 200 206 304 301 302 1d; # 緩存的key--》請求路徑 proxy_cache_key $request_uri; add_header X-Cache-Status $upstream_cache_status; proxy_pass http://127.0.0.1:8080; index index.html index.htm; } }
操作系統(tǒng)優(yōu)化
vi /etc/sysctl.conf
- 配置內容
# 防止一個套接字過多連接到達時引起負載 net.ipv4.tcp_syncookies=1 #默認128,socket的監(jiān)聽隊列,微調大 net.core.somaxconn=1024 # timeout的超時時間,調小,tcp握手時間 net.ipv4.tcp_fin_timeout=10 #os直接使用timewait的連接 net.ipv4.tcp_tw_reuse=1 #回收禁用,若開啟---》快速回收處于 TIME_WAIT狀態(tài)的socket net.ipv4.tcp_tw_recycle=0
- 加載配置!
sysctl -p
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Nginx+keepalived實現(xiàn)七層的負載均衡的高可用(最新解決方案)
這篇文章主要介紹了Nginx+keepalived實現(xiàn)七層的負載均衡的高可用,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03