站長(zhǎng)資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        CentOS 7.6配置Nginx反向代理

        一,實(shí)驗(yàn)介紹

        利用三臺(tái)CentOS 7虛擬機(jī)搭建簡(jiǎn)單的Nginx反向代理負(fù)載集群,三臺(tái)虛擬機(jī)地址及功能介紹

        192.168.2.76    nginx負(fù)載均衡器

        192.168.2.82    web01服務(wù)器

        192.168.2.78    web02服務(wù)器

        二,安裝nginx軟件(以下操作三臺(tái)虛擬機(jī)都要進(jìn)行)

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

        yum -y install wget

        安裝nginx軟件:(三個(gè)服務(wù)器都要安裝)

        $ 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安裝)

        安裝就這么簡(jiǎn)單方便,安裝完成后,就可以使用systemctl來控制nginx的啟動(dòng)了

        $ systemctl enable nginx (加入開機(jī)啟動(dòng))
        $ systemctl start nginx (開啟nginx)
        $ systemctl status nginx (查看狀態(tài))

        三臺(tái)服務(wù)器分別安裝好nginx后測(cè)試能否正常運(yùn)行,提供web服務(wù)。如果報(bào)錯(cuò)可能是防火墻的原因,請(qǐng)看最后幾步關(guān)于防火墻的。

        修改代理服務(wù)器的nginx的配置文件,實(shí)現(xiàn)負(fù)載均衡。顧名思義就是將多個(gè)請(qǐng)求分發(fā)到不同的服務(wù)上,實(shí)現(xiàn)均衡的負(fù)載,減小單個(gè)服務(wù)的壓力。

        $ 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; (默認(rèn)為自動(dòng),可以自己設(shè)置,一般不大于cpu核數(shù))
        error_log /var/log/nginx/error.log; (錯(cuò)誤日志路徑)
        pid /run/nginx.pid; (pid文件路徑)

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

        events {
            accept_mutex on; (設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on)
            multi_accept on; (設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接,默認(rèn)為off)
            worker_connections 1024; (一個(gè)進(jìn)程的最大連接數(shù))
        }

        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; (連接超時(shí)時(shí)間)
            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;

        # 這里設(shè)置負(fù)載均衡,負(fù)載均衡有多中策略,nginx自帶的有輪詢,權(quán)重,ip-hash,響應(yīng)時(shí)間等粗略。
        # 默認(rèn)為平分http負(fù)載,為輪詢的方式。
        # 權(quán)重則是按照權(quán)重來分發(fā)請(qǐng)求,權(quán)重高的負(fù)載大
        # ip-hash,根據(jù)ip來分配,保持同一個(gè)ip分在同一臺(tái)服務(wù)器上。
        # 響應(yīng)時(shí)間,根據(jù)服務(wù)器都nginx 的響應(yīng)時(shí)間,優(yōu)先分發(fā)給響應(yīng)速度快的服務(wù)器。
        集中策略可以適當(dāng)組合
            upstream tomcat { (tomcat為自定義的負(fù)載均衡規(guī)則名)
                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;

         

        ## 可以定義多組規(guī)則
        }

         

            server {
                listen      80 default_server; (默認(rèn)監(jiān)聽80端口)
                listen      localhost; (監(jiān)聽的服務(wù)器)
                server_name  _;
                root        /usr/share/nginx/html;

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

                location / { ( /  表示所有請(qǐng)求,可以自定義來針對(duì)不同的域名設(shè)定不同負(fù)載規(guī)則 和服務(wù))
          proxy_pass    http://tomcat; (反向代理,填上你自己的負(fù)載均衡規(guī)則名)
          proxy_redirect off; (下面一些設(shè)置可以直接復(fù)制過去,不要的話,有可能導(dǎo)致一些 沒法認(rèn)證等的問題)
          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; (下面這幾個(gè)都只是一些超時(shí)設(shè)置,可不要)
                  proxy_send_timeout 90;
                  proxy_read_timeout 90;
                }
          # location ~.(gif|jpg|png)$ { (比如,以正則表達(dá)式寫) 
          #  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 {
        #        }
        #    }
        }

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

        nginx -s reload

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

        啟動(dòng): systemctl start firewalld
        關(guān)閉: systemctl stop firewalld
        查看狀態(tài): systemctl status firewalld
        開機(jī)禁用  : systemctl disable firewalld
        開機(jī)啟用  : systemctl enable firewalld

        開啟一個(gè)端口:

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

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)
        主站蜘蛛池模板: 亚洲一区精品伊人久久伊人| 97久久精品人妻人人搡人人玩| 亚洲日韩精品无码一区二区三区| 91精品国产91久久| 老司机性色福利精品视频| 精品国产91久久久久久久a| 国产精品久久久久久影院| 三上悠亚久久精品| 亚洲一区无码精品色| 日韩一区二区三区在线精品| 国产综合免费精品久久久| 91精品国产91久久久久久青草| 国产精品天天影视久久综合网| 麻豆亚洲AV永久无码精品久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 国产亚洲欧美精品永久| 亚洲福利精品一区二区三区 | 亚洲日韩国产AV无码无码精品 | 国产亚洲精品a在线观看| 精品综合久久久久久97超人| 久久亚洲中文字幕精品有坂深雪| 亚洲欧美一级久久精品| 麻豆国产高清精品国在线| 国产精品免费一区二区三区| 四虎精品成人免费观看| 久久精品中文字幕久久| 嫩草影院久久国产精品| 国产福利微拍精品一区二区| 久久精品国产亚洲av高清漫画| 亚洲日韩精品无码专区网址| 中文字幕精品无码久久久久久3D日动漫| 欧美成人精品一区二三区在线观看| 国产精品无码一区二区在线| 国产精品成人99久久久久91gav| 91精品福利在线观看| 99久久99久久精品国产| 国产精品永久免费| 久久国产精品免费一区| 免费精品国自产拍在线播放| 久久亚洲国产精品123区| 亚洲精品成a人在线观看|