Nginx配置优化参考
???????
Nginx配置優化參考? ??????????? ??????????? ??????????? ??????????? ??????????? ??????????? ?????????????????? ???????
編輯詞條 ??????? ??????????? ??????????????? ?????????????????? ??????????????? ??????????????? ??????????????? ??????????????? ??????????????? ?????????????????????????? ???????
???????????
???????????
???????????
???????????
??????????? ???????????????????????????
摘要:本文檔描述了Nginx性能測試過程中,發現的可優化性能的配置項。主要包含系統參數,以及Nginx本身配置。本文可以作為后續上線以及維護過程中的參考文檔。 1、 nofile 系統打開的文件數約束。 Linux服務器必須修改這個值才能滿足Nginx的要求。建議修改為102400或者更大。 修改文件 /etc/security/limits.conf 默認值 1024 參考配置 * soft nofile 102400 * hard nofile 102400 2、 net.ipv4.ip_local_port_range 本地端口使用范圍,在Ng
???????????????????????
??????????? ??? ????
- ???????????????????
- ??????????????????????????? 1、??????nofile ???????????????????????
- ??????????????????????????? 2、??????net.ipv4.ip_local_port_range ???????????????????????
- ??????????????????????????? 3、??????其它sysctl.conf配置 ???????????????????????
- ??????????????????????????? 4、??????worker_processes ???????????????????????
- ??????????????????????????? 5、??????worker_cpu_affinity ??????????????????????? ???????????????
??????????????? ??????????????? [顯示全部] ???????????
??????????? ??????????? ??????? ????? ???
??????? ???????
本文檔描述了Nginx性能測試過程中,發現的可優化性能的配置項。主要包含系統參數,以及Nginx本身配置。本文可以作為后續上線以及維護過程中的參考文檔。
??????????????? [ 編輯本段 ] ??????????????????? ??????????? ????????????? ??[ 回目錄 ]1、??????nofile??? ??????????? ??????????? ?????????????????????? 系統打開的文件數約束。 Linux服務器必須修改這個值才能滿足Nginx的要求。建議修改為102400或者更大。
| 修改文件 | /etc/security/limits.conf |
| 默認值 | 1024 |
| 參考配置 | * soft nofile 102400 * hard nofile 102400 |
本地端口使用范圍,在Nginx作為web服務器時這個參數可以忽略,但作為反向代理服務器必須修改為更大范圍,高并發時可以大幅度提升Nginx性能。
| 修改文件 | /etc/sysctl.conf |
| 默認值 | 32768 61000 |
| 參考配置 | net.ipv4.ip_local_port_range ? = 1024 65000 |
| 修改文件 | /etc/sysctl.conf |
| 參考配置 | net.core.somaxconn = 2048 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_fin_timeout = ? 15 net.ipv4.tcp_max_tw_buckets ? = 10000 net.ipv4.ip_local_port_range ? = 1024 65000 fs.file-max = 102400 #與nofile保持一致 net.ipv4.tcp_mem = ? 1048576? 1310720 1572864 net.ipv4.tcp_rmem =? 4096 87380 16777216 net.ipv4.tcp_wmem =? 4096 87380 16777216 net.core.rmem_max = 16777216 net.core.wmem_max = ? 16777216 net.ipv4.tcp_synack_retries ? = 3 net.ipv4.tcp_syn_retries = ? 3 net.ipv4.tcp_max_syn_backlog=8192 |
| ? | ?使用/sbin/sysctl -p來做配置刷新 |
Nginx工作進程數,經過測試以及官方的建議最好配置為與CPU核數目相等,或者auto。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/ngx_core_module.html#worker_processes |
| 參考配置 | worker_processes 8; |
綁定Nginx工作進程到CPU核,可以使CPU負載更加均衡穩定。配置值與worker_processes以及CPU核數目有關。配置1對應綁定的核。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity |
| 參考配置 | worker_cpu_affinity ? 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; |
Nginx工作進程能打開的文件句柄數目,建議與nofile一致。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/ngx_core_module.html#worker_rlimit_nofile |
| 參考配置 | worker_rlimit_nofile 102400; |
Nginx工作進程能同時打開的鏈接數目,建議與worker_rlimit_nofile一致。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/ngx_core_module.html#worker_connections |
| 參考配置 | worker_connections 102400; |
Nginx訪問日志,一般情況下建議關閉,如果不能關閉就配置緩存可以大幅度提升IO性能。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/http/ngx_http_log_module.html#access_log |
| 參考配置 |
Nginx作為反向代理服務器時,upstream設置keepalive可以大幅度提升代理性能。如果是HTTP服務需要同時配置upstream和location。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive |
| 參考配置 | upstream http_backend { ??? server 127.0.0.1:8080; ??? keepalive 32; } server { ??? ... ??? location /http/ { ??????? proxy_pass http://http_backend; ??????? proxy_http_version 1.1; ??????? proxy_set_header Connection ? ""; ??????? ... ??? } } |
Nginx反向代理的服務需要會話保持功能,在real-server個數變化的時候影響盡可能小,可以配置一致性哈希。
| 修改文件 | nginx.conf |
| 官方說明 | http://nginx.org/en/docs/http/ngx_http_upstream_module.html#hash |
| 參考配置 | upstream ngx-backend-normal ? { ??? server 10.151.161.130:8080; ??? server 10.151.161.131:8080; ??? keepalive 32; ?? ?hash $remote_addr consistent; } |
?
轉載于:https://www.cnblogs.com/kabi/p/7986210.html
總結
以上是生活随笔為你收集整理的Nginx配置优化参考的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle所有分析函数转
- 下一篇: android.util.Base64