LNMP-源码 nginx ,keepalived
每次安裝源碼都會有一些忘記的模塊開啟;和需要插件;
上次完全做已經多年了,在總結下;
nginx1.10.0
安裝目錄 /usr/local/nginx
*為支持rewrite安裝pcre
# yum install pcre* -y
# yum installl openssl*
yum install gcc gcc-c++ gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel -y
yum install –y autoconf automake imake libxml* expat-devel cmake libaio libaio-devel bzr bison libtool* ncurses5-devel zlib*
# tra -xf nginx-1.10.1.tar.gz ?
# cd nginx-1.10.1
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre?
# yum install make
# make
# make install
查看安裝信息;
# /usr/local/nginx/sbin/nginx -V
啟動 nginx?
# /usr/local/nginx/sbin/nginx
直接使用curl命令來讀取web信息
# curl -s http://localhost | grep nginx.com
# lsof -i :80
# /usr/local/nginx/sbin/nginx -s stop
# lsof -i :80
重啟
# /usr/local/nginx/sbin/nginx -s reload
links 127.0.0.1
Nginx高并發的優化配置:
?
內核參數的優化 net.ipv4.tcp_max_tw_buckets = 6000 ?
timewait的數量,默認是180000。?
net.ipv4.ip_local_port_range = 1024 ? ?65000 ?
允許系統打開的端口范圍。
net.ipv4.tcp_tw_recycle = 1 ?
啟用timewait快速回收。
net.ipv4.tcp_tw_reuse = 1 ??
開啟重用。允許將TIME-WAIT sockets重新用于新的TCP連接。
net.ipv4.tcp_syncookies = 1 ??
開啟SYN Cookies,當出現SYN等待隊列溢出時,啟用cookies來處理。
net.core.somaxconn = 262144 ??
web應用中listen函數的backlog默認會給我們內核參數的
net.core.somaxconn限制到128,
而nginx定義的NGX_LISTEN_BACKLOG默認為511,所以有必要調整這個值。
net.core.netdev_max_backlog = 262144 ??
每個網絡接口接收數據包的速率比內核處理這些包的速率快時,允許送到隊列的數據包的最大數目。?
net.ipv4.tcp_max_orphans = 262144 ??
系統中最多有多少個TCP套接字不被關聯到任何一個用戶文件句柄上。如果超過這個數字,孤兒連接將即刻被復位并打印出警告信息。這個限制僅僅是為了防止簡單的DoS***,不能過分依靠它或者人為地減小這個值,更應該增加這個值(如果增加了內存之后)。
net.ipv4.tcp_max_syn_backlog = 262144 ??
記錄的那些尚未收到客戶端確認信息的連接請求的最大值。對于有128M內存的系統而言,缺省值是1024,小內存的系統則是128。
net.ipv4.tcp_timestamps = 0 ??
時間戳可以避免序列號的卷繞。一個1Gbps的鏈路肯定會遇到以前用過的序列號。時間戳能夠讓內核接受這種"異常"的數據包。這里需要將其關掉。
net.ipv4.tcp_synack_retries = 1 ??
為了打開對端的連接,內核需要發送一個SYN并附帶一個回應前面一個SYN的ACK。也就是所謂三次握手中的第二次握手。這個設置決定了內核放棄連接之前發送SYN+ACK包的數量。
net.ipv4.tcp_syn_retries = 1 ??
在內核放棄建立連接之前發送SYN包的數量。
net.ipv4.tcp_fin_timeout = 1 ??
如果套接字由本端要求關閉,這個參數決定了它保持在FIN-WAIT-2狀態的時間。對端可以出錯并永遠不關閉連接,甚至意外當機。缺省值是60秒。2.2 內核的通常值是180秒,你可以按這個設置,但要記住的是,即使你的機器是一個輕載的WEB服務器,也有因為大量的死套接字而內存溢出的風險,FIN- WAIT-2的危險性比FIN-WAIT-1要小,因為它最多只能吃掉1.5K內存,但是它們的生存期長些。
net.ipv4.tcp_keepalive_time = 30 ??
當keepalive起用的時候,TCP發送keepalive消息的頻度。缺省是2小時。
nginx + keepalived 負載均衡配置參考;
[root@localhost ~]# cat /usr/local/nginx/conf/nginx.conf
#user ?nobody;
worker_processes ?24; ?## 根據實際情況配置
events {
? ? worker_connections ?204800;
}
http {
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
? ? sendfile ? ? ? ?on;
? ? keepalive_timeout ?30;
? ? tcp_nopush ? ? ?on;
? ? gzip ?on;
? ? gzip_min_length ? 10k;?
? ? gzip_buffers ? 16 128k;
? ? gzip_http_version ?1.1;
? ? gzip_comp_level 6;
? ? gzip_vary on;
? ? gzip_types text/plain ?text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
? ? gzip_proxied any;
? ? gzip_disable "MSIE [1-6]\.";
? ? server_names_hash_bucket_size 1024;
? ? client_header_buffer_size 1024k;
? ? large_client_header_buffers 4 1024k;
? ? client_max_body_size 3000m;
? ? proxy_redirect off;
? ? proxy_set_header Host $http_host;
? ? proxy_set_header X-Real-IP $remote_addr;
? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
? ? proxy_connect_timeout 90;
? ? proxy_send_timeout 90;
? ? proxy_read_timeout 90;
? ? proxy_buffer_size 128k;
? ? proxy_buffers 4 128k;
? ? proxy_busy_buffers_size 128k;
? ? proxy_temp_file_write_size 128k;
? ? upstream server # 自定義?
????{?
# ? ? server ip:port;?
? ? ?server 192.168.1.11:80;
? ? ?server 192.168.1.12:80;
? ? ?server 192.168.1.13:80;
? ? ?server 192.168.1.14:80;
? ? ?server 192.168.1.15:80;
? ? ? ? }
? ? server {
? ? ? ? listen ? ? ? 80;
? ? ? ? server_name ?localhost;
? ? location / {
? ? ? ? ? ?
? ? ? ? root ? html;
? ? ? ? index ?index.html index.htm;
? ? ? ? proxy_pass http://server;?# upstream 自定義的
? ? ? ? proxy_set_header Host $host:80;
? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? proxy_set_header X-Forwarded-For ?$remote_addr;
? ? ? ? proxy_set_header Via "nginx";
? ? ? ? break;
? ? ? ? }
? ? ? ? error_page ? 500 502 503 504 ?/50x.html;
? ? ? ? location = /50x.html {
? ? ? ? ? ? root ? html;
? ? ? ? }
? ? location ~ .*\.(jpg|js|html|mp3|gif|jpeg|png|bmp|swf|ico|css)$
????{?
????????expires ? ? ?7d;?
????}?
? ? location ~ .*\.(js|css)?$?
????{?
????????expires ? ? ?24h;?
????}
? ? }
}
轉載于:https://blog.51cto.com/51log/1832111
總結
以上是生活随笔為你收集整理的LNMP-源码 nginx ,keepalived的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux基础知识(一)
- 下一篇: win8 远程桌面时提示凭证不工作问题的