Linux開發(fā)環(huán)境之Nginx

介紹

Nginx("engine x")是一款是由俄羅斯的程序設(shè)計(jì)師Igor Sysoev所開發(fā)高性能的 Web和 反向代理 服務(wù)器,也是一個(gè) IMAP/POP3/SMTP 代理服務(wù)器。在高連接并發(fā)的情況下,Nginx是Apache服務(wù)器不錯(cuò)的替代品。

環(huán)境

Nginx 安裝
系統(tǒng)平臺(tái):CentOS release 6.8 (Final) 32位。

安裝開始

一、安裝編譯工具及庫(kù)文件

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

二、首先要安裝 PCRE

PCRE 作用是讓 Nginx 支持 Rewrite 功能。

1、下載 PCRE 安裝包,下載地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

[root@bogon server]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
2、解壓安裝包:

[root@bogon server]# tar zxvf nginx-1.6.2.tar.gz

3、進(jìn)入安裝包目錄

[root@bogon server]# cd nginx-1.6.2

4、編譯安裝

[root@bogon nginx-1.6.2]# ./configure --prefix=/usr/local/server/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/server/pcre-8.35
[root@bogon nginx-1.6.2]# make
[root@bogon nginx-1.6.2]# make install

5、查看pcre版本

[root@bogon nginx-1.6.2]# /usr/local/server/nginx/sbin/nginx -v

到此,nginx安裝完成。

檢查配置文件ngnix.conf的正確性命令:

[root@bogon conf]# /usr/local/server/nginx/sbin/nginx -t

啟動(dòng) Nginx
Nginx 啟動(dòng)命令如下:

[root@bogon conf]# /usr/local/server/nginx/sbin/nginx

Nginx 其他命令

以下包含了 Nginx 常用的幾個(gè)命令:

/usr/local/server/nginx/sbin/nginx -s reload            # 重新載入配置文件
/usr/local/server/nginx/sbin/nginx -s reopen            # 重啟 Nginx
/usr/local/server/nginx/sbin/nginx -s stop              # 停止 Nginx

附加(保留用戶IP到真實(shí)服務(wù)器)

說明:這是后期附加內(nèi)容,需求是獲取用戶請(qǐng)求的IP地址。但是經(jīng)過Nginx代理后的請(qǐng)求,執(zhí)行g(shù)etRemoteAddr()方法時(shí),獲取到的是本機(jī)IP,造成存儲(chǔ)了本機(jī)的IP信息,而不是用戶的IP。在查閱各種資料后,得出以下解決辦法。

前往Nginx安裝目錄下/conf/路徑,找到nginx.conf配置文件,在代理配置項(xiàng)添加以下內(nèi)容。

#此處配置請(qǐng)求路徑,這里為  ' / ',即根路徑
location / {
            proxy_set_header Host $host;                #保留主機(jī)信息
            proxy_set_header X-Real-IP $remote_addr;    #保留IP信息
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Remote_Addr $http_true_client_ip;
            proxy_redirect off;
            proxy_pass   http://127.0.0.1:8091/;        #請(qǐng)求由誰(shuí)處理?就在這里配置
        }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容