nginx搭建反向代理配置


一、安裝gcc

   yum install gcc-c++ -y

 

二、安裝nginx所需要的依賴庫

   yum -y install zlib-devel openssl-devel pcre-devel

 

三、如有安裝老版本,則卸載。

   查看:find -name nginx

   卸載:yum remove nginx


四、下載nginx源碼並解壓.(源碼去官網下載, 以下是官方鏈接)

    wget -c http://nginx.org/download/nginx-1.6.2.tar.gz

    tar -zxvf nginx-1.6.2.tar.gz

    mv nginx-1.6.2 nginx

    cd nginx

    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx

    --with-http_addition_module

    --with-http_flv_module --with-http_gzip_static_module

    --with-http_realip_module --with-http_ssl_module

    --with-http_stub_status_module --with-http_sub_module

    --with-http_dav_module

注:這里--with開頭的選項為nginx自帶的模塊,需要什么就添加,默認是不安裝,

 

    make

    make install


五、建立nginx用戶及用戶組

      groupadd -r nginx

    useradd  -s /sbin/nologin -g nginx -r nginx
  
cd 到nginx目錄,看能否啟動
sbin/nginx           #如果沒有報錯,說明ok

再ps一下,ps ax | grep nginx,看到如下,說明ok
               2537 ?        Ss     0:00 nginx: master process sbin/nginx
               2538 ?        S      0:00 nginx: worker process

           nginx在啟動后,會有一個master進程和多個worker進程。master進程主要用來管理worker進程,包含:接收來自外界的信號,向各worker進程發送信號,監控worker進程的運行狀態,當worker進程退出后(異常情況下),會自動重新啟動新的worker進程。

 查看安裝的nginx版本:
            sbin/nginx -v
 查看安裝的編譯選項:
           sbin/nginx -V


六、下面開始配置nginx,及反向代理,編輯配置文件nginx.conf
    vim /usr/local/nginx/conf/nginx.conf

 

   user nginx nginx;                                   #這里是nginx運行的用戶

   worker_processes 2;                            #設置nginx服務的worker子進程,比如設為2:

   error_log logs/error.log;                        #去掉前面的#,記錄nginx錯誤日志,方便檢查bug:

   pid logs/nginx.pid;                                 #nginx的pid位置

 

events {
             worker_connections  1024;       #每個進程允許的最多連接數,
 }

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;                      #日志存放位置

 

#這里很關鍵,很多小伙伴問我 “負載均衡乍配置,為啥我配置的不能訪問呢“,這里的upstream就是配置負載均衡的,當然得兩台以上才叫負載,我這里的ip69和68都是

#用的apache,   也許你們的是tomcat, 沒關系,按這樣配置一樣可以,

 upstream proxy_test {

   server 192.168.4.69:80 weight=1;     #如果你要測試,把這里換成你自己要代理后端的ip

   server 192.168.4.68:80 weight=1;

   #ip_hash;                                              #當負載兩台以上用ip來hash解決session的問題,一台就別hash了。

 }

這是server段的配置

server {

    listen       80;

    server_name  www.test.com;    #要訪問的域名,我這里用的測試域名,如果有多個,用逗號分開

 

    charset utf8;

 

    location / {

        proxy_pass       http://proxy_test;               #這里proxy_test是上面的負載的名稱,映射到代理服務器,可以是ip加端口,   或url 

        proxy_set_header Host      $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      }

   }

}
保存退出!

nginx平滑重啟:nginx -s reload   #加載剛剛加入的配置。

 

配置nginx開機自啟動

vim /etc/rc.d/rc.local


 

 


免責聲明!

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



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