nginx工作小結(1)
阿新 • • 發佈:2018-12-10
Nginx命令列
預設啟動方式,直接使用二進位制程式,讀取配置檔案conf/nginx.conf
/usr/local/nginx/sbin/nginx
指定配置檔案的啟動方式,使用-c引數後指定的nginx.conf配置檔案來啟動nginx
/usr/local/nginx/sbin/nginx -c /tmp/nginx.conf
另行指定安裝目錄的啟動方式
/usr/local/nginx/sbin/nginx -p /tmpr/nginx/
可以通過-g引數臨時指定一些全域性配置項,以使新的配置生效
/usr/local/nginx/sbin/nginx -g “pid /var/nginx/test.pid;” ## g引數的約束條件 一是指定的配置項不能與預設路徑下的nginx.conf中國的配置項相沖突,否則無法啟動; 二是以-g方式啟動的Nginx服務執行其他命令列時,需要把-g引數也帶上,如停止nginx服務 /usr/local/nginx/sbin/nginx -g “pid /var/nginx/test.pid;” -s stop
測試配置資訊是否有錯誤
# 在不啟動nginx情況下,使用-t引數僅測試配置檔案是否有錯誤
/usr/local/nginx/sbin/nginx -t
# 測試階段不輸出資訊
/usr/local/nginx/sbin/nginx -t -q
顯示版本資訊
/usr/local/nginx/sbin/nginx -v
顯示編譯階段的引數
/usr/local/nginx/sbin/nginx -V
快速停止服務
/usr/local/nginx/sbin/nginx -s stop
kill -s SIGTERM nginx_pid(nginx master pid)
優雅停止服務
/usr/local/nginx/sbin/nginx -s quit
只停止某個worker程序
kill -s SIGTERM <nginx worker pid>
使執行中的nginx重讀配置並生效
/usr/local/nginx/sbin/nginx -s reload
kill -s SIGHUP <nginx master pid>
日誌檔案回滾
/usr/local/nginx/sbin/nginx -s reopen
kill -s SIGUSR1 <nginx master pid>
平滑升級nginx
kill -s SIGUSR2 <nginx master pid> 此時會將nginx的pid檔案重新命名,如將/usr/local/nginx/logs/nginx.pid rename為 /usr/local/nginx/logs/nginx.pid.oldbin 使用上述啟動命令開啟新的Nginx服務,這時通過ps命令可發現新舊版本的Nginx在同時執行 通過kill命令向舊版本的master程序傳送SIGQUIT訊號,以”優雅”的方式關閉舊版本的Nginx
顯示命令列幫助
/usr/local/nginx/sbin/nginx -h
Nginx常用配置
nginx基本配置
http{
gzip on;
# 配置負載均衡
upstream service{
server 192.168.10.221:9091 weight 2;
server 192.168.10.221:9092 weight 3;
server 192.168.10.221:9093 weight 2;
}
# ip hash負載
upstream backend {
ip_hash;
server backend1. example.com;
server backend2. example.com;
server backend3. example.com down; #down表示上游伺服器永久下線,只在使用ip_hash配置項時才有用
server backend4. example.com;
}
# 配置多個虛擬主機server
server{
listen [ip]:80;
location /webstatic{
}
location ~*.(jpg|jpeg|png|jpe|gif)${
}
}
}
靜態資源配置
location ~* /static/*.+\.(gif|jpg|png|css|js|flv|ico|swf)$ {
alias /home/ubuntu/mywork/python/youhuiquan/website/static;
expires 1d;
# proxy_pass http://static;
# proxy_redirect off;
# proxy_cache_valid 200 302 1h;
# proxy_cache_valid 301 1h;
# proxy_cache_valid any 1h;
add_header Pragma public;
add_header Cache-Control "public";
}
移動端配置
location / {
if ( $http_user_agent ~ "(MIDP)|(WAP)|(UP.Browser)|(Smartphone)|(Obigo)|(Mobile)|(AU.Browser)|(wxd.Mms)|(WxdB.Browser)|(CLDC)|(UP.Link)|(KM.Browser)|(UCWEB)|(SEMC\-Browser)|(Mini)|(Symbian)|(Palm)|(Nokia)|(Panasonic)|(MOT\-)|(SonyEricsson)|(NEC\-)|(Alcatel)|(Ericsson)|(BENQ)|(BenQ)|(Amoisonic)|(Amoi\-)|(Capitel)|(PHILIPS)|(SAMSUNG)|(Lenovo)|(Mitsu)|(Motorola)|(SHARP)|(WAPPER)|(LG\-)|(LG/)|(EG900)|(CECT)|(Compal)|(kejian)|(Bird)|(BIRD)|(G900/V1.0)|(Arima)|(CTL)|(TDG)|(Daxian)|(DAXIAN)|(DBTEL)|(Eastcom)|(EASTCOM)|(PANTECH)|(Dopod)|(Haier)|(HAIER)|(KONKA)|(KEJIAN)|(LENOVO)|(Soutec)|(SOUTEC)|(SAGEM)|(SEC\-)|(SED\-)|(EMOL\-)|(INNO55)|(ZTE)|(iPhone)|(Android)|(Windows CE)|(Wget)|(Java)|(curl)|(Opera)" ){
rewrite ^.+ http://m.domainName.com/$uri;
}
rewrite ^.+ http://www.domainName.com/$uri;
}
服務負載均衡基本配置
location / {
proxy_pass http://service;
#proxy_method GET | POST
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; # 獲取真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 獲取代理者的真實IP
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
# proxy_redirect http://service http://service_others; # service返回302或301的時候將會重定向到service_others路由
# proxy_next_upstream[ error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_404 | off]; # 當上遊伺服器出錯的時候將繼續轉發到另一臺上游伺服器處理相同的請求
client_max_body_size 60m;
}
uWSGI 配置
upstream django {
# server unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
}
php fastcgi配置
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9001;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php;
#root /home/crazysal/public_html;
fastcgi_pass 127.0.0.1:9001;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
nginx解決圖片資源沒有後綴名訪問時直接下載問題
location / {
root /path/static;
add_header content-type "image/jpeg";
expires 1d;
// ...其他相關的配置
}
nginx 配置錯誤碼跳轉頁面
# 當服務端出現以下的狀態碼的時候將會跳轉到50x的頁面,此時還需要配置50x的頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal; # 客戶端無法直接訪問,必須是由nginx伺服器內部轉發的
root errors/html;
}
# 為location配置一個名稱 (也是隻能內部轉發)
error_page 500 502 503 504 = @errors;
location @errors {
proxy_pass http://error_backend;
}
# location 查詢順序和優先順序
1. 帶有 = 精確匹配優先
2. 沒有修飾符的精確匹配優先
3. 正則表示式按照它們在配置檔案中定義的順序
4. 帶有 ^~ 修飾的開頭匹配
5. 帶有 ~ 或者 ~* 修飾的,如果正則與URI匹配
6. 沒有修飾的,指定字串與URI開頭匹配
try_files
# 按照指定的順序檢查存在的檔案,並且返回第一個找到的檔案結果,如果所有檔案都沒有找到,那麼將啟用最後一個引數命名的內部重定向
upstream render_uri{
server 127.0.0.1:9080 weight=9;
}
try_files path1 path2 @render
location @render{
proxy_pass http://render_uri;
}
加證書SSL配置支援https
server {
listen 443;
listen [::]:443;
charset UTF-8;
server_name domain;
gzip on;
gzip_types application/json; # text/html,application/json格式總是會被壓縮
ssl on;
ssl_certificate /etc/nginx/ssl-certs/xx.crt;
ssl_certificate_key /etc/nginx/ssl-certs/xx.key;
error_log /home/ubuntu/java/logs/nginx/nginx_error.log;
access_log /home/ubuntu/java/logs/nginx/nginx_access.log main_log;
root /home/ubuntu/test;
index index.html;
location /album {
proxy_pass http://album;
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_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
client_max_body_size 60m;
}
location /dounile/images {
root /home/ubuntu/java/dounile/images;
expires 1d;
}
location /dounile {
proxy_next_upstream http_502 http_504 timeout; # 配置故障轉移,如果服務出現502,504或者是timeout則將轉發給下一個
valid_referers blocked dtreess.com *.dtreess.com; # 配置簡單的防盜鏈,只有dtreess.com的域名下才能訪問
proxy_pass http://dounile;
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_set_header X-Forwarded-Proto $http_x_forwarded_proto;
proxy_set_header Via "nginx";
client_max_body_size 60m;
}
}
全域性負載均衡
http{
geo $geo{
default default;
203.101.10.10/24 A; # 客戶端滿足IP段走A伺服器
109.23.23.10/28 B; # 客戶端滿足IP段走B伺服器
}
upstream default.server {
server 192.168.10.1 weight=10;
}
upstream A.server{
server 192.168.10.2 weight=10;
}
upstream B.server{
server 192.168.10.3 weight=10;
}
server {
listen 80;
location / {
proxy_pass: http://$geo.server/$request_uri;
}
}
}
rewrite 模組
# 執行url的重定向,有利於去掉惡意訪問的url,也有利於搜尋引擎優化
last: 完成重寫指令,之後搜尋相應的URI或location
break:完成重寫指令
redirect:返回302的臨時重定向,如果替換欄位用http://開頭則被使用
permanent:返回301永久重定向
nginx檢視程序進行追蹤
# 開啟shell終端
strace -f pid
# 開啟shell第二個終端
nginx -s reload
# 在上面的strace將跟蹤程序啟動過程並輸出到控制檯中
設定nginx常用的日誌格式
log_format log_main_format '$ remote_addr - $ remote_user [$ time_local] $ request ' 'upstream_response_time $ upstream_response_time ' 'msec $ msec request_time $ request_time'; log_format up_head '$ remote_addr - $ remote_user [$ time_local] $ request ' 'upstream_http_content_type $ upstream_http_content_type';
nginx優化配置
1. 儘量提高單臺機器的處理效率
2. 儘量降低單臺機器的負載
3. 降低磁碟IO
4. 降低網路IO
5. 減少記憶體使用
6. 高效利用CPU
# nginx.config
user keithl;
work_processes 8;
http{
events{
use epoll;
# worker_connections的設定與實體記憶體有關,因為系統可以開啟的最大檔案數與記憶體成正比,一般1G記憶體機器可以開啟的檔案數是10萬個
worker_connections 1024; # 併發總數 = work_processes * worker_connections,需要根據設定程序數和系統可以開啟的檔案數進行調整
}
}