由於目前項目中服務可能是用nginx進行均衡負載的,因此這裏記錄下,爲之後備查。html
以下:nginx
nginx安裝目錄:/root/softwate/nginx後端
nginx可執行文件目錄:/root/softwate/nginx/sbin/nginxcentos
nginx配置文件目錄:/root/softwate/nginx/conf/nginx.conf瀏覽器
nginx的配置文件以下:服務器
#用戶,通常不用配置,我測試用的root 須要指定下root user root; #user nobody; #啓動進程數量,通常和主機內核數量一致 worker_processes 1; #錯誤日誌打印 #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { #單個後臺worker process進程的最大併發連接數 worker_connections 1024; } http { #設定mime類型,類型由mime.type文件定義 include mime.types; default_type application/octet-stream; #設置日誌格式 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; #sendfile sendfile: 設置爲on表示啓動高效傳輸文件的模式 #對於普通應用,設爲 on, #若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off, #這個參數不開啓,會先在用戶空間(Nginx進程空間)申請一個buffer #用read函數把數據從磁盤讀到cache,再從cache讀取到用戶空間的buffer #再用write函數把數據從用戶空間的buffer寫入到內核的buffer,最後到tcp socket #開啓這個參數後可讓數據不用通過用戶buffer sendfile on; #tcp_nopush on; #鏈接超時時間 #keepalive_timeout 0; keepalive_timeout 65; #開啓gzip壓縮 #gzip on; #upstream服務器,輪詢機制,均衡負載,以權重的方式分發,weight是權重 #權重越大則分配的可能行越大 #這裏後端服務只有一個,因此都會分發到哪個上 upstream test { server 172.21.0.5:8081 weight=10; } #虛擬主機配置 server { #監聽80端口 listen 80; #定義訪問地址,能夠有多個,用空格分隔 server_name localhost; #charset koi8-r; #虛擬主機的訪問日誌 #access_log logs/host.access.log main; #默認請求 location / { #root html; #默認訪問目錄 root /root/html; #指定默認訪問頁面 index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } #配置訪問靜態文件 #配置訪問url中包含test的靜態文件 location ~ ^/test/.*\.(gif|bmp|jpg|jpeg|png|swf|GIF|BMP|JPG|JPEG|PNG)$ { #文件目錄 root /root/html; } #啓用反向代理,配置服務 #分發處理帶有test的url請求 location ~ /(test)/.*$ { #分發到upstream服務test ,也能夠直接指定http://ip:port 如172.21.0.5:8081 proxy_pass http://test; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Host $host:$server_port; #後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header connect ""; #如下可選,爲反向代理配置 client_max_body_size 30m; #容許客戶端請求的最大單文件字節數 client_body_buffer_size 1024k; #緩衝區代理緩衝用戶端請求的最大字節數 proxy_connect_timeout 60; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) proxy_send_timeout 120; #後端服務器數據回傳時間(代理髮送超時) proxy_read_timeout 120; #鏈接成功後,後端服務器響應時間(代理接收超時) proxy_buffer_size 128k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 proxy_buffers 8 128k; #proxy_buffers緩衝區,網頁平均在32k如下的設置 proxy_busy_buffers_size 128k; #高負荷下緩衝大小(通常proxy_buffers*2) } } }
nginx服務的操做命令:併發
如下命令都是按照我安裝的nginx目錄操做,具體要根據本身的實際修改。app
一、驗證nginx的配置是否有問題。在啓動nginx或者從新加載ng配置以前,要檢測nginx的配置文件是否正常。特別是線上通常都是從新加載ng配置的,防止出問題,必定要檢測。socket
命令:/root/softwate/nginx/sbin/nginx -t -c /root/softwate/nginx/conf/nginx.conftcp
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -t -c /root/softwate/nginx/conf/nginx.conf nginx: the configuration file /root/softwate/nginx/conf/nginx.conf syntax is ok nginx: configuration file /root/softwate/nginx/conf/nginx.conf test is successful [root@VM_0_5_centos sbin]#
二、啓動nginx。
命令:/root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf
啓動後,通常沒有提示,能夠自行查看nginx進程
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf [root@VM_0_5_centos sbin]# [root@VM_0_5_centos sbin]# ps -ef | grep nginx root 30262 1 0 10:32 ? 00:00:00 nginx: master process /root/softwate/nginx/sbin/nginx -c /root/softwate/nginx/conf/nginx.conf root 30263 30262 0 10:32 ? 00:00:00 nginx: worker process root 30267 29966 0 10:33 pts/0 00:00:00 grep --color=auto nginx [root@VM_0_5_centos sbin]#
三、從新加載nginx配置。通常修改nginx配置後,從新加載便可,不須要重啓。
命令:root/softwate/nginx/sbin/nginx -s reload
[root@VM_0_5_centos sbin]# /root/softwate/nginx/sbin/nginx -s reload [root@VM_0_5_centos sbin]#
從新加載後,通常也沒有直接提示,但在是ng的日誌中,會有一條日誌記錄。總之,不熟悉的話,線上要修改前,要作好備份和配置文件校驗。
測試:
在nginx默認訪問目錄/root/html下,生成index.html文件。就是測試,內容隨便啦。。。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>nginx測試首頁</title> </head> <body> <p>這是ng訪問測試</p> </body> </html>
而後,瀏覽器訪問ng對應的外網ip和端口口號80(端口也能夠不加). 瀏覽器正常顯示nginx測試首頁,說明配置正常。
其餘的服務均衡負載測試,就要和本身服務配合着來了,這裏就不放了。