Centos 7.6配置nginx反向代理,直接yum安裝


一,實驗介紹

利用三台centos7虛擬機搭建簡單的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

 




免責聲明!

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



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