最近學了下nginx 的負載平衡原理,動手來配一個執行個體練練手。
1. 前言
我用的環境是 centos7, nginx版本是 1.8.1,具體怎麼安裝,參考我前面的文章。我公司的應用伺服器有個環境,一個是開發環境,一個是測試環境,兩個環境的應用是一樣的,但是庫裡的資料是不同的,這樣也正好方便等會兒的測試。
2. 配置nginx負載平衡
nginx預設是以 conf/nginx.conf 作為啟動配置的,conf/nginx.conf.default是作為nginx.conf的一個備份,兩個檔案的內容完全一樣,這樣我們可以根據自己的需求在nginx.conf 中配置負載平衡,nginx.conf 內容如下:
#user nobody; #使用哪個使用者啟動nginx 前邊是使用者 後邊是組 worker_processes1; # nginx 背景工作處理序資料量(通常為伺服器的cpu核心數)# [debug | info | warn | error | crit] 錯誤記錄檔的層級及位置#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;# 進程檔案#pid logs/nginx.pid;events { # 每個進程的最大串連數worker_connections1024;}# 設定http伺服器,利用它的反向 Proxy實現負載平衡支援http { include mime.types; # 設定mime類型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;sendfileon; #開啟高效檔案傳輸模式# 以下兩個選項用於防止網路阻塞#tcp_nopush on;#tcp_nodelay on;##tcp_nopush 這個參數只有 sendfile on 的時候才有用。tcp_nodelay 只在 keepalive 串連狀態中使用。# 逾時時間keepalive_timeout65; # 開啟gzip模組#gzip on;# 負載平衡配置upstream myproject { # 預設以輪詢策略server192.168.1.111; #開發環境ipserver192.168.1.114; #測試環境ip } # 虛擬Proxy 伺服器配置server { listen80; # 伺服器名稱,隨便起名server_name nginx_proxy; #charset koi8-r;#access_log logs/host.access.log main;location / { # root html;# index index.html index.htm;#設定主機頭和用戶端真真實位址,以便伺服器擷取用戶端真實IPproxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁用緩衝proxy_bufferingoff; # 反向 Proxy的地址proxy_passhttp://myproject; } #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;#}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {# proxy_pass http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {# root html;# fastcgi_pass 127.0.0.1:9000;# fastcgi_index index.php;# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;# include fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#} } # another virtual host using mix of IP-, name-, and port-based configuration##server {# listen 8000;# listen somename:8080;# server_name somename alias another.alias;# location / {# root html;# index index.html index.htm;# }#}# HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}}
3. Nginx常用命令
#測試nginx配置: nginx -t /usr/local/nginx/conf/fzjh.conf #啟動、關閉 ./sbin/nginx # 預設設定檔 conf/nginx.conf,-c 指定設定檔啟動 ./sbin/nginx -s stop 或 pkill nginx #重啟,不會改變啟動時指定的設定檔 ./sbin/nginx -s reload
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了Nginx 負載平衡配置執行個體,包括了nginx,負載平衡方面的內容,希望對PHP教程有興趣的朋友有所協助。