不少程序員面試被問到nginx優化作過哪些,所以來記錄下。javascript
配置文件樣例爲生產環境樣例。php
安裝方式有2種:css
區別爲若是用yum安裝的話,很方便,而且基本不報錯。若是對性能要求不是很高的話,能夠採用這種安裝方式(好比測試環境)。java
若是是源碼包安裝的話,由於在服務器上編譯的軟件,會讓nginx的性能相對更高一些,建議生產環境使用源碼包安裝。node
基本配置優化(優化後配置樣例,能夠改後直接上生產)nginx
#頭部配置 user nginx nginx; #定義nginx的啓動用戶,不建議使用root worker_processes 4; #定位爲cpu的內核數量,由於個人環境配置是4核,因此就寫4。不過這值最多也就是8,8個以上也就沒什麼意義了,想繼續提高性能只能參考下面一項配置 worker_cpu_affinity 0001 0010 0100 1000; #此項配置爲開啓多核CPU,對你先弄提高性能有很大幫助nginx默認是不開啓的,1爲開啓,0爲關閉,所以先開啓第一個倒過來寫, 第一位0001(關閉第四個、關閉第三個、關閉第二個、開啓第一個) 第二位0010(關閉第四個、關閉第三個、開啓第二個、關閉第一個) 第三位0100(關閉第四個、開啓第三個、關閉第二個、關閉第一個) 後面的依次類推,有智商的應該均可以看懂了吧? 那麼若是是16核或者8核cpu,就注意爲0000000一、000000十、00000100,總位數與cpu核數同樣。 error_log /data/logs/nginx/error.log crit; #這兩項基本不用我說 pid /usr/local/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; #這個值爲nginx的worker進程打開的最大文件數,若是不配置,會讀取服務器內核參數(經過ulimit -a查看),若是內核的值設置過低會讓nginx報錯(too many open file),可是在此設置後,就會讀取本身配置的參數不去讀取內核參數 events { use epoll; #客戶端線程輪詢方法、內核2.6版本以上的建議使用epoll worker_connections 65535; #設置一個worker能夠打開的最大鏈接數 } http { include mime.types; default_type application/octet-stream; #charset gb2312; server_tokens off; #爲錯誤頁面上的nginx版本信息,建議關閉,提高安全性 server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 8m; sendfile on; #開啓sendfile()函數,sendfile能夠再磁盤和tcp socket之間互相copy數據。 tcp_nopush on; #告訴nginx在數據包中發送全部頭文件,而不是一個一個的發 #keepalive_timeout 15; keepalive_timeout 120; tcp_nodelay on; proxy_intercept_errors on; fastcgi_intercept_errors on; fastcgi_connect_timeout 1300; fastcgi_send_timeout 1300; fastcgi_read_timeout 1300; fastcgi_buffer_size 512k; fastcgi_buffers 4 512k; fastcgi_busy_buffers_size 512k; fastcgi_temp_file_write_size 512k; proxy_connect_timeout 20s; proxy_send_timeout 30s; proxy_read_timeout 30s; gzip on; #gzip是告訴nginx採用gzip後的數據來傳輸文件,會大量減小咱們的發數據的量 gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; gzip_vary on; gzip_disable msie6; #limit_zone crawler $binary_remote_addr 10m; log_format main '$http_host $remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '$request_time $upstream_response_time'; #proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區,由於它們之間是硬連接的關係 #proxy_temp_path /var/cache/nginx/proxy_temp_dir; #設置Web緩存區名稱爲cache_one,內存緩存空間大小爲200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小爲30GB。 #proxy_cache_path /var/cache/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; include /usr/local/nginx/conf/vhosts/*.conf; error_page 404 = https://www.niu.com/404/; #error_page 500 502 503 504 = http://service.niu.com/alien/; }
若是是高併發架構,須要在nginx的服務器上添加以下的內核參數。程序員
這些參數追加到/etc/sysctl.conf,而後執行sysctl -p 生效。面試
每一個網絡接口接收數據包速度比內核處理速度快的時候,容許發送隊列數目數據包的最大數。緩存
net.core.netdev_max_backlog = 262144
調節系統同時發起的tcp鏈接數安全
net.core.somaxconn = 262144
該參數用於設定系統中最多容許存在多少TCP套接字不被關聯到任何一個用戶文件句柄上,主要目的爲防止Ddos攻擊
net.ipv4.tcp_max_orphans = 262144
該參數用於記錄還沒有收到客戶端確認信息的鏈接請求的最大值
net.ipv4.tcp_max_syn_backlog = 262144
nginx服務上建議關閉(既爲0)
net.ipv4.tcp_timestamps = 0
該參數用於設置內核放棄TCP鏈接以前向客戶端發送SYN+ACK包的數量,爲了創建對端的鏈接服務,服務器和客戶端須要進行三次握手,第二次握手期間,內核須要發送SYN並附帶一個迴應前一個SYN的ACK,這個參數主要影響這個過程,通常賦予值爲1,即內核放棄鏈接以前發送一次SYN+ACK包。
net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1
二、nginx配置lua,添加接口返回值,方便開發debug
三、nginx配置https
#server端基本配置<br>server { listen 80; listen 443 ssl spdy; server_name io.123.com; include ssl/io.com; #注意看下一個文件 location / { proxy_pass http://lb_io; if ($scheme = http ) { return 301 https://$host$request_uri; #此項配置爲轉換爲https的基本配置 } proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } access_log /data/logs/nginx/access/niuaero.log main; } ssl_certificate ssl/ca/io.com.pem; #這個爲購買的https證書,供應商會生成 ssl_certificate_key ssl/ca/io.com.key; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #啓用TLS1.一、TLS1.2要求OpenSSL1.0.1及以上版本,若您的OpenSSL版本低於要求,請使用 ssl_protocols TLSv1; ssl_ciphers HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM; ssl_prefer_server_ciphers on;
四、nginx配置反爬蟲
#如下內容添加nginx虛擬主機配置裏,proxypass以後<br><br>if ($http_user_agent ~* (Scrapy|Curl|HttpClient)) { return 403; } #禁止指定UA及UA爲空的訪問 if ($http_user_agent ~ "WinHttp|WebZIP|FetchURL|node-superagent|java/|FeedDemon|Jullo|JikeSpider|Indy Library|Alexa Toolbar|AskTbFXTV|AhrefsBot|CrawlDaddy|Java|Feedly|Apache-HttpAsyncClient|UniversalFeedParser|ApacheBench|Microsoft URL Control|Swiftbot|ZmEu|oBot|jaunty|Python-urllib|lightDeckReports Bot|YYSpider|DigExt|HttpClient|MJ12bot|heritrix|EasouSpider|Ezooms|BOT/0.1|YandexBot|FlightDeckReports|Linguee Bot|^$" ) { return 403; } #禁止非GET|HEAD|POST方式的抓取 if ($request_method !~ ^(GET|HEAD|POST)$) { return 403; }