站長資訊網
最全最豐富的資訊網站

CentOS 7.6配置Nginx反向代理

一,實驗介紹

利用三臺CentOS 7虛擬機搭建簡單的Nginx反向代理負載集群,三臺虛擬機地址及功能介紹

192.168.2.76    nginx負載均衡器

192.168.2.82    web01服務器

192.168.2.78    web02服務器

二,安裝nginx軟件(以下操作三臺虛擬機都要進行)

有些Centos 7.6里面沒有安裝wget命令,所以要自己安裝:

yum -y install wget

安裝nginx軟件:(三個服務器都要安裝)

$ wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

$ rpm -ivh epel-release-latest-7.noarch.rpm

$ yum install nginx (直接yum安裝)

安裝就這么簡單方便,安裝完成后,就可以使用systemctl來控制nginx的啟動了

$ systemctl enable nginx (加入開機啟動)
$ systemctl start nginx (開啟nginx)
$ systemctl status nginx (查看狀態)

三臺服務器分別安裝好nginx后測試能否正常運行,提供web服務。如果報錯可能是防火墻的原因,請看最后幾步關于防火墻的。

修改代理服務器的nginx的配置文件,實現負載均衡。顧名思義就是將多個請求分發到不同的服務上,實現均衡的負載,減小單個服務的壓力。

$ vi /etc/nginx/nginx.conf  (修改配置文件,全局配置文件)

 

# For more information on configuration, see:
#  * Official English Documentation: http://nginx.org/en/docs/
#  * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto; (默認為自動,可以自己設置,一般不大于cpu核數)
error_log /var/log/nginx/error.log; (錯誤日志路徑)
pid /run/nginx.pid; (pid文件路徑)

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    accept_mutex on; (設置網路連接序列化,防止驚群現象發生,默認為on)
    multi_accept on; (設置一個進程是否同時接受多個網絡連接,默認為off)
    worker_connections 1024; (一個進程的最大連接數)
}

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;
    gzip on; (開啟壓縮)
    include            /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

# 這里設置負載均衡,負載均衡有多中策略,nginx自帶的有輪詢,權重,ip-hash,響應時間等粗略。
# 默認為平分http負載,為輪詢的方式。
# 權重則是按照權重來分發請求,權重高的負載大
# ip-hash,根據ip來分配,保持同一個ip分在同一臺服務器上。
# 響應時間,根據服務器都nginx 的響應時間,優先分發給響應速度快的服務器。
集中策略可以適當組合
    upstream tomcat { (tomcat為自定義的負載均衡規則名)
        ip_hash; (ip_hash則為ip-hash方法)

      server 192.168.2.78:80 weight=3 fail_timeout=20s;
      server 192.168.2.82:80 weight=4 fail_timeout=20s;

 

## 可以定義多組規則
}

 

    server {
        listen      80 default_server; (默認監聽80端口)
        listen      localhost; (監聽的服務器)
        server_name  _;
        root        /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / { ( /  表示所有請求,可以自定義來針對不同的域名設定不同負載規則 和服務)
  proxy_pass    http://tomcat; (反向代理,填上你自己的負載均衡規則名)
  proxy_redirect off; (下面一些設置可以直接復制過去,不要的話,有可能導致一些 沒法認證等的問題)
  proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_connect_timeout 90; (下面這幾個都只是一些超時設置,可不要)
          proxy_send_timeout 90;
          proxy_read_timeout 90;
        }
  # location ~.(gif|jpg|png)$ { (比如,以正則表達式寫) 
  #  root /home/root/images;
  #  }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen      443 ssl http2 default_server;
#        listen      [::]:443 ssl http2 default_server;
#        server_name  _;
#        root        /usr/share/nginx/html;
#
#        ssl_certificate “/etc/pki/nginx/server.crt”;
#        ssl_certificate_key “/etc/pki/nginx/private/server.key”;
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
}

更新配置后,可以重載配置生效,不需要重啟服務

nginx -s reload

如果不能訪問,可能是由于防火墻打開了,端口沒有開啟:

啟動: systemctl start firewalld
關閉: systemctl stop firewalld
查看狀態: systemctl status firewalld
開機禁用  : systemctl disable firewalld
開機啟用  : systemctl enable firewalld

開啟一個端口:

添加
firewall-cmd –zone=public –add-port=80/tcp –permanent    (–permanent永久生效,沒有此參數重啟后失效)
重新載入
firewall-cmd –reload
查看
firewall-cmd –zone= public –query-port=80/tcp
刪除
firewall-cmd –zone= public –remove-port=80/tcp –permanent

贊(0)
分享到: 更多 (0)
網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
亚洲日韩国产一区二区三区在线 | 亚洲av日韩av天堂影片精品| 中文字幕精品一区二区 | 日韩精品人妻一区二区中文八零 | av蓝导航精品导航| 亚洲午夜精品一级在线播放放| 亚洲av永久中文无码精品综合| 久久亚洲日韩看片无码| 国产精品黄大片在线播放| 亚洲国产美女精品久久久| 国产精品一二三区| 亚洲av产在线精品亚洲第一站| 精品无码AV一区二区三区不卡 | 国产福利专区精品视频| 黑人粗长大战亚洲女2021国产精品成人免费视频 | 久久99国产精品成人| 国产精品视频色拍拍| 精品亚洲永久免费精品| 久久国产乱子伦精品免费强| 精品国产美女福利到在线不卡| 亚洲精品乱码久久久久66| 国产极品白嫩精品| 99精品无人区乱码1区2区3区| 三上悠亚国产精品一区| 中文字幕在线不卡精品视频99| 久久免费国产精品| 99精品免费视频| av蓝导航精品导航| 日本精品久久久中文字幕| 亚洲国产精品福利片在线观看| 久久国产精品免费专区| 午夜DY888国产精品影院| 人妻AV一区二区三区精品 | 亚洲精品日韩一区二区小说| 日韩va亚洲va欧洲va国产| 亚洲日韩精品无码专区加勒比 | 日韩精品久久久久久久电影| 亚洲国产精品日韩在线| 中文字幕无码亚洲欧洲日韩| 老司机福利精品视频| 国产精品手机在线观看你懂的|