日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡

發布時間:2025/3/16 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

# 用了nginx for win很久,安裝也是超級簡單。
# 還是用一下linux版的吧。環境是centos 6.5 x64

# 安裝開始: # 先安裝依賴 yum install gcc-c++ yum -y install pcre* yum -y install openssl*# 下載,可以wget 目前最新1.15.3 cd /opt wget http://nginx.org/download/nginx-1.12.2.tar.gztar zxf nginx-1.12.2.tar.gz cd nginx-1.12.2# 指定安裝目錄 、編譯安裝 ./configure --prefix=/opt/nginx make && make install# 檢查測試 /opt/nginx/sbin/nginx -t

?

# 啟動? 停止? 退出?

/opt/nginx/sbin/nginx /opt/nginx/sbin/nginx -s stop /opt/nginx/sbin/nginx -s quit
# 如果是centos7以上,已經注冊為系統服務的:
systemctl stop nginx
systemctl start nginx #---------------- 官方文檔: -s 參數------------ # stop — fast shutdown # quit — graceful shutdown # reload — reloading the configuration file # reopen — reopening the log files #----------------------------------------------

?

# 查看進程,端口? 檢查運行情況

ps aux |grep nginx # master worker 至少各一個 netstat -tulnp | grep :80# 如果想要命令方便執行,可將路徑加入PATH變量中 vim /etc/profile # 加入 2 行export NGINX_HOME=/opt/nginx export PATH=$NGINX_HOME/sbin:$PATHsource /etc/profile # 生效

?

#-------------------- 配置文件 nginx.conf ---------------------------

以下版本有一些簡單的優化, 注意那些寫有注釋的行。

user nginx; worker_processes auto; # 進程數,一般設置為CPU核數,或者 auto pid /var/run/nginx.pid; # 記錄進程PID文件,可以不啟用# Load dynamic modules. See /usr/share/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf;events { use epoll; # 使用高效的 epoll 方式 worker_connections 65535; # 單個worker進程同時打開的最大連接數 跟系統的 ulimit -n 有關 }http { 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; server_tokens off; # 隱藏web出錯時的版本號

?

虛擬主機?

超簡單配置 基于不同域名的虛擬主機,其實就是:根據訪問的不同網址對應不同的nginx中的不同文件夾。

先備份一個conf/nginx.conf文件,然后修改 http{}中的server, 刪除原有的,示例如下:

## 注意:下面僅僅是http{}部分,不是nginx.conf的全部 http {server_tokens on; include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;server { # 創建服務listen 80;server_name wwww.node-6.com; # 域名## location 匹配URL路徑地址,/ 表示匹配所有路徑 =開頭表示精確匹配 ~區分大小寫 ~*不區分大小寫(默認不區分)location / { root data/www; # 根目錄對應真實文件夾index index.html index.htm index.jsp;}}server {listen 80;server_name bbs.node-6.com;location / {root data/bbs;index index.html index.htm index.jsp;}}}

對應地,需要在nginx下建立目錄 data/www 和 data/bbs 以及各自目錄下的文件。

執行 nginx -s reload 重新載入配置文件。 配置好DNS解析或hosts文件,瀏覽器測試訪問。

?

超簡單配置 基于不同端口號的虛擬主機,其實就是:根據訪問網址的不同端口 對應不同的nginx中的不同文件夾。

和上面的配置差不多,只不過域名相同,listen不同:

server { # 服務listen 80;server_name wwww.node-6.com; # 域名location / { root data/www; # 根目錄對應真實文件夾index index.html index.htm index.jsp;}} server {listen 81;server_name wwww.node-6.com;location / {root data/bbs;index index.html index.htm index.jsp;}}server {listen 82;server_name wwww.node-6.com;location / {root data/img;index index.html index.htm index.jsp;}}

同樣,nginx -s reload 即可生效。

?

配置?nginx 反向代理

和上面的虛擬主機配置差不多,既可以不同端口,也可以不同域名,關鍵詞?proxy_pass??示例如下:

## 不同端口的反向代理 server{}部分server { # 服務listen 80;server_name wwww.node-6.com; # 域名location / { proxy_pass http://127.0.0.1:8080; # 反向代理本地 tomcatindex index.html index.htm index.jsp;}} server {listen 81;server_name www.node-6.com; # 域名location / {proxy_pass http://127.0.0.1:8081; # 反向代理index index.html index.htm index.jsp;}} ## 不同域名的反向代理 server{}部分server { # 服務listen 80;server_name wwww.node-6.com; # 域名location / { proxy_pass http://127.0.0.1:8080; # 反向代理本地 tomcatindex index.html index.htm index.jsp;}} server {listen 80;server_name bbs.node-6.com; # 域名location / {proxy_pass http://127.0.0.1:8081; # 本地另一個 tomcatindex index.html index.htm index.jsp;}}

配置完成后,同樣,nginx -s reload 即可生效。

?

不同Location 做反向代理,示例如下:

## 不同Location的反向代理 server{}部分 ## 注意目標地址末尾要有/server { # 服務listen 80;server_name www.node-6.com; # 域名location /www { # 末尾有無/不影響proxy_pass http://127.0.0.1:8080/; # 末尾一定要/index index.html index.htm index.jsp;}location /bbs {proxy_pass http://127.0.0.1:8081/; index index.html index.htm index.jsp;}}

?

負載均衡:

使用 upstream 方式,配置也很簡單:在server{} 上面定義一個 upstream?backServer 然后在proxy_pass中指向backServer? 示例如下:

## 以下部分全部應在 http{}內: ## 定義多個上游服務器(真實業務)服務器的IP和端口,默認采用輪詢機制upstream backServer{server 127.0.0.1:8080;server 192.168.112.5:8080;}server {listen 80;server_name www.node-6.com; # 域名location / {proxy_pass http://backServer/; # 末尾一定要/index index.html index.htm index.jsp;}}

負載均衡的方式:

輪詢機制:輪流訪問,非常均勻

權重機制:使用weight配置比例

ip hash: nginx獲取IP地址hash運算固定分配到某服務器上,可以解決session共享問題

fair :第三方

url綁定:第三方?

?

權重機制的設置和上面的簡單設置差不多,只在目標后面加了weight。示例如下:

upstream backServer{server 127.0.0.1:8080 weight=1;server 192.168.112.5:8080 weight=3;}

?

IP綁定方式,只是多了行 ip_hash;?

upstream backServer{server 127.0.0.1:8080;server 192.168.112.5:8080;ip_hash;}

?

一般情況下,可能實際生產環境,配置輪詢機制或者權重機制比較多見。

如果上游服務器有個突然掛了怎么辦??

所以,要設置好nginx的故障轉移,示例如下:

## http{}中的兩段配置
upstream backServer{server 127.0.0.1:8080;server 127.0.0.1:8081;server 192.168.112.5:8080;}server {listen 80;server_name www.node-6.com; # 域名location / {proxy_pass http://backServer/; # 末尾一定要/## 故障轉移:設置超時時間proxy_connect_timeout 1s;proxy_send_timeout 1s;proxy_read_timeout 1s;index index.html index.htm index.jsp;}}

?

URL重寫:

參考:http://www.cnblogs.com/czlun/articles/7010604.html

  ? https://www.linuxidc.com/Linux/2014-01/95493.htm

?rewrite????<regex>????<replacement>????[flag];

關鍵字???? ?正則?????? ?替代內容???????? ?flag標記

?

? ? 。關鍵字:其中關鍵字error_log不能改變

? ? 。正則:perl兼容正則表達式語句進行規則匹配

? ? 。替代內容:將正則匹配的內容替換成replacement

? ? 。flag標記:rewrite支持的flag標記

?

flag標記說明:

last? #本條規則匹配完成后,繼續向下匹配新的location URI規則

break? #本條規則匹配完成即終止,不再匹配后面的任何規則

redirect? #返回302臨時重定向,瀏覽器地址會顯示跳轉后的URL地址

permanent? #返回301永久重定向,瀏覽器地址欄會顯示跳轉后的URL地址

?

rewrite參數的標簽段位置:

server, location, if

?

?實際配置文件示例:?nginx/default.conf

?

分類:?Server 標簽:?nginx,?epoll,?反向代理,?負載均衡,?故障轉移,?proxy_pass,?location,?tomcat,?url重寫

轉載于:https://www.cnblogs.com/FHBIAO/p/10174600.html

總結

以上是生活随笔為你收集整理的centos 6.5 下 nginx 简单优化_虚拟主机_负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。