一、Tomcat安裝 1.下載jdk,Tomcat,解壓到/usr/local/ 2.配置jdk環境:# vim /etc/profile export JAVA_HOME=/usr/local/jdk1.8.0_171 export PATH=$JAVA_HOME/bin:$PATH export ...
一、Tomcat安裝
1.下載jdk,Tomcat,解壓到/usr/local/
2.配置jdk環境:
1 # vim /etc/profile 2 export JAVA_HOME=/usr/local/jdk1.8.0_171 3 export PATH=$JAVA_HOME/bin:$PATH 4 export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:$CLASSPATH
3.修改Tomcat首頁:
# cd /usr/local/apache-tomcat-8.0.1/webapps/ # rm -rf !(ROOT) # rm -rf ROOT/* # echo "192.168.11.199" >ROOT/index.html
4.啟動Tomcat
二、nginx安裝
1.下載源碼包,解壓到/usr/local/
2.安裝編譯依賴:
1 # yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel
3.編譯:
1 # ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --with-http_ssl_module
4.安裝:
1 # make && make install
5.啟動:
1 # nginx
三、負載均衡
1.ssl認證
私鑰:
1 # openssl genrsa -des3 -out jason.key 1024
數字證書:
1 # openssl req -new -key jason.key -out jason.csr
去除訪問密碼:
1 # openssl rsa -in jason.key -out jason-np.key
公鑰:
1 # openssl x509 -req -days 366 -in jason.csr -signkey jason-np.key -out jason.crt
2.修改nginx配置文件:
1 # vim /opt/nginx/conf/nginx.conf 2 #全局配置 3 worker_processes 1; 4 pid /var/run/nginx.pid; 5 worker_rlimit_nofile 65535; 6 7 #events配置 8 events { 9 use epoll; 10 accept_mutex on; 11 multi_accept on; 12 worker_connections 1024; 13 } 14 15 #HTTP配置 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 sendfile on; 20 keepalive_timeout 65; 21 tcp_nopush on; 22 tcp_nodelay on; 23 client_header_buffer_size 32k; 24 large_client_header_buffers 4 64k; 25 client_max_body_size 8m; 26 proxy_cache_key '$host:$server_port$request_uri'; 27 proxy_temp_file_write_size 64k; 28 proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 29 fastcgi_connect_timeout 300; 30 fastcgi_send_timeout 300; 31 fastcgi_read_timeout 300; 32 fastcgi_buffer_size 64k; 33 fastcgi_buffers 4 64k; 34 fastcgi_busy_buffers_size 128k; 35 fastcgi_temp_file_write_size 128k; 36 fastcgi_intercept_errors on; #開啟錯誤頁面 37 log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; ' 38 '$status $body_bytes_sent "$http_referer" - $request_body' 39 '"$http_user_agent" "$http_x_forwarded_for"'; #設置日誌輸出格式 40 error_log /var/log/nginx/error.log; 41 access_log /var/log/nginx/access.log main; #成功日誌格式調用log_format 42 gzip on; #開啟頁面壓縮,提高頁面打開速度 43 gzip_min_length 1k; 44 gzip_buffers 16 64K; 45 gzip_http_version 1.1; 46 gzip_comp_level 6; 47 gzip_types text/plain application/x-javascript text/css application/xml application/javascript; 48 gzip_vary on; 49 50 51 upstream tomcat_server 52 { 53 server 192.168.11.199:8080 weight=1; 54 server 192.168.11.196:8080 weight=2; 55 } 56 57 server 58 { 59 listen 443; 60 server_name 192.168.11.199; 61 ssl on; 62 ssl_certificate /opt/nginx/ssl/nginx.crt; #公鑰 63 ssl_certificate_key /opt/nginx/ssl/nginx.key; #私鑰 64 ssl_session_timeout 5m; 65 66 location / 67 { proxy_pass http://tomcat_server; } #負載 68 } 69 70 server 71 { 72 listen 70; 73 root html; 74 index index.html index.htm; 75 76 } 77 78 server 79 { 80 listen 71; 81 root html; 82 index index.html; 83 error_page 404 = /404.html; 84 error_page 500 502 503 504 = /50x.html; 85 } 86 }
3.編寫日誌分割腳本
1 #!/bin/bash 2 #此腳本用於自動分割Nginx的日誌,包括access.log和error.log 3 #每天00:00執行此腳本 將前一天的access.log重命名為access-xxxx-xx-xx.log格式,並重新打開日誌文件 4 #Nginx日誌文件所在目錄 5 LOG_PATH=/var/log/nginx/ 6 #獲取昨天的日期 7 YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) 8 #獲取pid文件路徑 9 PID=/var/run/nginx.pid 10 #分割日誌 11 mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log 12 mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log 13 #向Nginx主進程發送USR1信號,重新打開日誌文件 14 kill -USR1 `cat ${PID}` 15 chmod +x /opt/nginx/conf/log_cut.sh
4.相關測試
1.gzip壓縮測試:
1 # curl -I -H "Accept-Encoding: gzip, deflate" "192.168.11.199"
2.nginx版本號:F12查看頁面
3.日誌分割:
1 # ll /var/log/nginx
4.日誌輸出格式:
1 # tail -f /var/log/nginx/access-2018-11-13.log
5.錯誤頁面:
1 http://192.168.11.199:71/djkf
5.壓力測試(ApacheBench):
1 # yum -y install httpd-tools 2 關閉ssl認證測試: 3 # ab -c 500 -n 20000 http://192.168.11.199:80/index.html #一次5000併發,請求總數為200000 4 Concurrency Level: 500 #一次請求量 5 Time taken for tests: 10.484 seconds #耗時 6 Complete requests: 20000 #完成請求 7 Failed requests: 6666 #請求失敗 8 9 10 11 開啟ssl認證測試: 12 # ab -c 500 -n 20000 https://192.168.11.199:443/index.html 13 Concurrency Level: 500 14 Time taken for tests: 27.011 seconds 15 Complete requests: 20000 16 Failed requests: 6667
6.nginx調優:
1 worker_rlimit_nofile 65535; #文件打開數量 2 worker_connections 65535; #單個進程最大連接數 3 sendfile on; #開啟高效文件傳輸模式 4 tcp_nopush on; #防止網路阻塞 5 fastcgi_connect_timeout 600; #指定連接到後端FastCGI的超時時間。 6 fastcgi_send_timeout 600; #向FastCGI傳送請求的超時時間。 7 fastcgi_read_timeout 600; #指定接收FastCGI應答的超時時間。
7..系統層面
文件資源限制的配置:
1 # vim /etc/security/limits.conf 2 * soft nofile 65535 3 * hard nofile 65535 4 * soft noproc 65535 5 * hard noproc 65535
#logout重新登錄查看
1 # ulimit -n
2.內核參數:
1 # vim /etc/sysctl.conf 2 net.ipv4.ip_forward = 0 #出現禁用 IPv4 包轉送 3 net.ipv4.conf.default.rp_filter = 1 #源路由核查功能 4 net.ipv4.conf.default.accept_source_route = 0 #禁用所有IP源路由 5 kernel.sysrq = 0 #禁用SysRq(組合鍵)功能 6 kernel.core_uses_pid = 1 #控制core文件的文件名中是否添加pid作為擴展 7 net.ipv4.tcp_syncookies = 1 //這四行標紅內容,一般是發現大量TIME_WAIT時的解決辦法 8 kernel.msgmnb = 65536 #每個消息隊列的最大位元組限制。 9 kernel.msgmax = 65536 #整個系統的最大數量的消息隊列 10 kernel.shmmax = 68719476736 #定義單個共用記憶體段的最大值 11 kernel.shmall = 4294967296 #控制共用記憶體頁數 12 net.ipv4.tcp_max_tw_buckets = 6000 13 net.ipv4.tcp_sack = 1 #啟用有選擇的應答 14 net.ipv4.tcp_window_scaling = 1 #設置tcp/ip會話的滑動視窗大小是否可變 15 net.ipv4.tcp_rmem = 4096 87380 4194304 #為每個TCP連接分配的讀、寫緩衝區記憶體大小 16 net.ipv4.tcp_wmem = 4096 16384 4194304 #為每個TCP連接分配的讀、寫緩衝區記憶體大小 17 net.core.wmem_default = 8388608 # 發送套接字緩衝區大小的預設值 18 net.core.rmem_default = 8388608 #接收套接字緩衝區大小的預設值 19 net.core.rmem_max = 16777216 20 net.core.wmem_max = 16777216 21 net.core.netdev_max_backlog = 262144 #每個網路介面接收數據包的速率比內核處理這些包的速率快時,允許送到隊列的數據包的最大數目 22 net.core.somaxconn = 262144 23 net.ipv4.tcp_max_orphans = 3276800 24 net.ipv4.tcp_max_syn_backlog = 262144 25 net.ipv4.tcp_timestamps = 1 //#開啟TCP時間戳,這個選擇最好加上 26 net.ipv4.tcp_synack_retries = 1 #服務端收到sys,還未發出syn+ack 27 net.ipv4.tcp_syn_retries = 1 28 net.ipv4.tcp_tw_recycle = 1 //開啟此功能可以減少TIME-WAIT狀態,但是NAT網路模式下打開有可能會導致tcp連接錯誤,慎重。 29 net.ipv4.tcp_tw_reuse = 1 30 net.ipv4.tcp_mem = 94500000 915000000 927000000 31 net.ipv4.tcp_fin_timeout = 30 32 net.ipv4.tcp_keepalive_time = 30 33 net.ipv4.ip_local_port_range = 1024 65000 34 net.ipv4.ip_conntrack_max = 6553500 35 # sysctl -p