Nginx配置反向代理,負載均衡,動靜分離,高可用的步驟


一、Nginx配置文件認識

1.配置文件位置

配置文件在 /usr/local/nginx/conf 下有個 nginx.conf 文件

2.Nginx配置文件組成

Nginx的配置文件由部分組成

  1. 第一部分 全局塊

設置一些影響Nginx服務器,整體運行的配置信息。

#user  nobody;
worker_processes  1; #Nginx處理並發的大小,受硬件約束(值越大,處理並發量越大)

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
  1. 第二部分events

設置Nginx服務器和用戶的網絡連接的配置信息。

events {
    worker_connections  1024; # Nginx 配置服務器最大連接數
}
  1. 的三部分 HTTP 塊(配置最頻繁的地方)

配置反向代理,動靜分離,負載均衡

http {
    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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;  # 端口號
        server_name  localhost; # 主機名稱

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            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;
        }

        # 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;
    #    }
    #}

}

二、Nginx配置相關實例

1.Nginx配置-反向代理

(1)目標

打開瀏覽器,在瀏覽器地址欄輸入地址 www.123.com,跳轉到 liunx 系統 tomcat 主頁

面中

(2)准備工作

1.安裝JDK

2.在linux中安裝並配置tomcat服務器,使用默認端口8080

3.開放8080端口

firewall-cmd --add-port=8080/tcp --permanent # 開放端口

firewall-cmd --reload # 重啟防火牆

安裝tomcat和JDK就不說了。

(3)配置

修改配置文件中 server下 server_name 為 訪問的服務器ip地址,給server下 location / 下添加 proxy_pass http://127.0.0.1:8080

然后重啟nginx就好了(運行命令必須在/usr/local/nginx/bin內)

#1.啟動nginx
./nginx
#2.關閉nginx
./nginx -s stop



① 目標

使用nginx反向代理,根據訪問的路徑跳轉到不同端口的服務中,nginx監聽端口為9001

訪問http://localhost:9001/etc/a.html 訪問8080端口的服務器

訪問http://localhost:9001/void/a.html 訪問8081端口的服務器

② 准備工作

准備兩個tomcat一個端口8080一個端口8081

在tomcat中創建顯示的文件(

808x

③ 配置

在nginx的配置文件中配置,(每條設置后的分號不要忘記 ";" )

server{
	listen  9001;
	server_name 127.0.0.1; #本機ip
	location ~ /void/ {
		proxy_pass http:127.0.0.1:8080;
	}
	location ~ /etc/ {
		proxy_pass http:127.0.0.1:8081;
	}
}

Nginx配置文件location說明

location 是有順序的,會被第一個匹配的location處理。

1.首先匹配=

2.其次匹配^~

3.再其次按照配置文件的順序進行正則匹配

4.最后是交給/進行通用匹配

提示:當有匹配成功時,立刻停止匹配,按照當前匹配規則處理請求

2.Nginx配置-負載均衡

(1)實現效果

1.在瀏覽器地址輸入:http://192.168.12.25/edu/a.html,負載均衡效果,平均到8080和8081端口去

(2)准備工作

1.准備兩台tomcat服務器,一台端口為8080一台為8081

2.在兩台tomcat里的webapp目錄中,創建文件夾edu,在edu文件夾創建頁面a.html 用於測試

(3)配置

在nginx的配置文件中配置,http 塊中配置 ,和server塊中配置

#http塊中添加此塊
upstream myserver{
	server 127.0.0.1:8080;
	server 127.0.0.1:8081;
}
#server塊中
配置端口號和本機ip
location / {
	proxy_pass http://myserver; #配置內容
}

Nginx負載均衡策略

1.輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。

2、指定權重(weight)

指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。 權重默認為1,權重越高,被分配的客戶端就越多(在http塊中配置的)

3、IP綁定 ip_hash

每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。

在配置文件加入ip_hash;

4、fair(第三方)

按后端服務器的響應時間來分配請求,響應時間短的優先分配。

在配置文件加入fair;

5、url_hash(第三方)

按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。

3.Nginx配置-動態分離

通過 location 指定不同的后綴名實現不同的請求轉發。通過 expires 參數設置,可以使瀏

覽器緩存過期時間,減少與服務器之前的請求和流量。具體 Expires 定義:是給一個資源

設定一個過期時間,也就是說無需去服務端驗證,直接通過瀏覽器自身確認是否過期即可,

所以不會產生額外的流量。此種方法非常適合不經常變動的資源。(如果經常更新的文件,

不建議使用 Expires 來緩存),我這里設置 3d,表示在這 3 天之內訪問這個 URL,發送一

個請求,比對服務器該文件最后更新時間沒有變化,則不會從服務器抓取,返回狀態碼 304,

如果有修改,則直接從服務器重新下載,返回狀態碼 200。

(1)准備工作

1.在linux系統中准備一些靜態資源,用於一會訪問測試。

/data/www/ 下存放html

/data/image/ 下 存放圖片

(2)配置

1.在nginx配置文件中配置

(3)測試

4.Nginx配置-高可用

(1)准備工作

需要多台服務器

在這幾台服務器上安裝nginx

在這幾個服務器上安裝keepalived

# 下載安裝 keepalived      安裝位置就在  /etc/keepalived
yum install keepalived -y
# 查看是否安裝成功
rpm -q -a keepalived

(2)開始配置

(1)配置文件在/etc/keepalived/keepalived.conf

global_defs {
   notification_email {
     acassen@firewall.loc # 郵箱
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc # 郵箱
   smtp_server 192.168.92.128 #服務器ip
   smtp_connect_timeout 30
   router_id LVS_DEVEL  #訪問到主機的名字 在/etc/hosts 添加 127.0.0.1 LVS_DEVEL
}

vrrp_script chk_http_port {

    script "/usr/local/src/nginx_check.sh"
    
    interval 2  #(檢測腳本執行的間隔)每隔兩秒檢測一次
    
    weight 2   # 權重
}

vrrp_instance VI_1 {
    state MASTER    #此服務器是主服務器就是MASTER,從服務器就是BACKUP
    interface ens33 #網卡 ifconfig 可以查看
    virtual_router_id 51 #主、備機的virtual_router_id必須相同
    priority 100  #主、備機取不同的優先級,主機值較大,備份機值較小 (優先級)
    advert_int 1 #每隔多久監測一次服務器是否存活
    authentication {
        auth_type PASS   
        auth_pass 1111
    }
    virtual_ipaddress {
    	# 虛擬ip可以配置多個
        192.168.92.150  //VRRP H虛擬機地址
    }
}

(2)在 /usr/local/src/ 下添加檢測腳本nginx_check.sh

配置文件中的 /server/nginx/sbin/nginx 是nginx的啟動位置

#!/bin/bash
A=`ps -C nginx -no-header |wc -1`
if [ $A -eq 0 ];then
    /server/nginx/sbin/nginx  
    sleep 2
    if [ `ps -C nginx --no-header |wc -1` -eq 0 ];then
        killall keepalived
    fi
fi

(3)啟動服務器和keepalived

systemctl start keepalived.service

(3)測試

訪問虛擬ip的地址


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM