以下就是我們整理的nginx常見的問題,解決辦法我們例舉了1-2種,大家可以都測(cè)試下。
常見問題
問題一:相同server_name多個(gè)虛擬主機(jī)優(yōu)先級(jí)訪問
server{ listen 80; server_name server1; location{...}}server{ listen 80; server_name server2; location{...}}解決方法:
配置兩個(gè)conf文件:server1.conf 和 server2.conf
根據(jù)Linux系統(tǒng)中文件順序讀取
問題二:location匹配優(yōu)先級(jí)
location = /code1/ { rewrite ^(.*)$ /code1/index.html break;}location ~ /code.* { rewrite ^(.*)$ /code3/index.html break;}location ^~ /code { rewrite ^(.*)$ /code2/index.html break;}知識(shí)填坑:
=:進(jìn)行普通字符精確匹配,完全匹配
^~:普通字符匹配,使用前綴匹配
~ /~*:表示執(zhí)行一個(gè)正則匹配()
解決方法:
根據(jù)匹配找到最優(yōu)匹配
優(yōu)先級(jí):完全匹配>正則匹配>前綴匹配
問題三:try_files使用
location / { try_files $uri $uri/ /index.html;}解決方法:
按順序檢查文件是否存在
問題四:Nginx的alias和root區(qū)別
location /request_path/img/ { root /local_path/img/;}location /request_path/img/ { alias /local_path/img/;}解決方法:
root設(shè)置,最終請(qǐng)求的路徑為/local_path/img/request_path/img/
alias設(shè)置,最終請(qǐng)求為/local_path/img/
問題五:通過多層代理,傳遞用戶真實(shí)IP
解決方法:
set x_real_ip=$remote_addr$x_real_ip=真實(shí)IP
性能優(yōu)化問題
優(yōu)化考慮點(diǎn):
當(dāng)前系統(tǒng)結(jié)構(gòu)瓶頸,如觀察指標(biāo)、壓力測(cè)試
了解業(yè)務(wù)模式,如接口業(yè)務(wù)類型、系統(tǒng)層次化結(jié)構(gòu)
性能與安全
接口壓力測(cè)試工具:ab
安裝:yum install httpd-tools
使用:ab -n 2000 -c 20 http://127.0.0.1/
nginx關(guān)于系統(tǒng)的優(yōu)化點(diǎn):
網(wǎng)絡(luò)、系統(tǒng)、服務(wù)、程序、數(shù)據(jù)庫
控制文件句柄數(shù)量,文件句柄就是一個(gè)索引
CPU親和,使進(jìn)程不會(huì)在處理器間頻繁遷移,減少性能損耗
vim /etc/nginx/nginx.confuser nginx;worker_processes 16;worker_cpu_affinity auto;worker_rlimit_nofile 15535;events{ use epoll; worker_connections 10240;}http{ include /etc/nginx/mime.types; default_type application/octet-stream; #Charset charset utf-8; log_format main ''; access_log /var/log/nginx/access.log main; #Core module sendfile on; keepalive_timeout 65; #Gzip module gzip on; gzip_disable "MSIE [1-6]/."; gzip_http_version 1.1; #Virtal server include /etc/nginx/conf.d/*.conf;}nginx安全問題及防范策略
惡意行為
問題:爬蟲行為和惡意抓取、資源盜用
解決方法:
基礎(chǔ)防盜鏈功能:不讓惡意用戶輕易的爬取網(wǎng)站對(duì)外數(shù)據(jù)
secure_link_module模塊:對(duì)數(shù)據(jù)安全性提高加密驗(yàn)證和失效性,對(duì)一些重要數(shù)據(jù)使用
新聞熱點(diǎn)
疑難解答
圖片精選