nginx之 nginx-1.9.7 編譯安裝、理論簡介

張衝andy發表於2017-06-14

nginx是一個web網站常用的高效能http和反向代理伺服器,其具有較好的併發能力,被網易、百度、騰訊、新浪等網站廣泛使用。

一、 理論簡介

1.首先弄清楚正向代理和反向代理

正向代理:代理客戶端,替客戶端收發請求,使真實的客戶端對伺服器不可見。如圖所示,proxy和client同屬於一個網路,對server透明


反向代理:代理伺服器,提伺服器收發請求,使真實的伺服器對客戶端不可見。如圖所示,proxy和server同屬於一個網路,對client透明

實際上proxy都是代為收發請求和響應,只是在結構上左右換了下,所以一個叫正向代理,另一個叫反向代理。

2.nginx的反向代理原理

如下圖所示:nginx作為反向代理伺服器接收來自客戶端的http請求,然後將請求轉發給內部網路的web伺服器,同時接收來自web伺服器的response結果,並返回給客戶端。此時nginx代理伺服器對外展現為一個伺服器。


3.nginx的反向代理的作用

(1)負載均衡。nginx可以將來自客戶端的請求均衡的分發到web伺服器叢集中的不同機器上進行處理,平衡叢集中各個伺服器的壓力。這對於大訪問量的web網站來說,是需要的。
(2)安全保障。客戶端直接訪問的不是提供內容的web伺服器,為保護網站伺服器提供了一層屏障,有利於保護網站的安全。
(3)加速web請求。nginx可以配置快取,儲存真實web伺服器的某些資源和響應,減輕真實伺服器的壓力,同時加速web請求


二、 nginx-1.9.7 編譯安裝

下載地址: http://nginx.org/download/nginx-1.9.7.tar.gz

補充: 安裝 nginx之前,需要先安裝一些依賴包:gcc、pcre、zlib
a、nginx gzip模組需要zlib庫
b、nginx rewrite模組需要pcre庫
c、nginx ssl模組需要openssl庫

1、安裝必要依賴包
[root@mysql03 ~]# yum install -y pcre pcre-devel

centos 6.7 配置 yum 本地源 連結參考: http://blog.csdn.net/zhang123456456/article/details/56690945 
2、 Nginx安裝
[root@mysql03 ~]# ll nginx-1.9.7.tar.gz 
-rw-r--r--. 1 root root 885562 Jun 14 21:46 nginx-1.9.7.tar.gz
[root@mysql03 ~]# tar zxvf nginx-1.9.7.tar.gz
[root@mysql03 ~]# cd nginx-1.9.7
-- 配置nginx安裝選項
[root@mysql03 nginx-1.9.7]# ./configure --prefix=/usr/local/nginx
說明: 配置完畢後可以看到一個配置概要,概要中的5項必須都有了相應的庫支援
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used 
##如果想要安裝openssl模組,安裝時需指定 ./configure --prefix=/usr/local/nginx --with-openssl=/root/openssl-1.0.2d ## 
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
-- 安裝nginx
[root@mysql03 nginx-1.9.7]# make && make install
3、 檢查安裝是否正常
[root@mysql03 nginx-1.9.7]# cd /usr/local/nginx
[root@mysql03 nginx]# ll
total 16
drwxr-xr-x. 2 root root 4096 Jun 14 22:14 conf
drwxr-xr-x. 2 root root 4096 Jun 14 22:14 html
drwxr-xr-x. 2 root root 4096 Jun 14 22:14 logs
drwxr-xr-x. 2 root root 4096 Jun 14 22:14 sbin
-- 啟動
[root@mysql03 nginx]# ./sbin/nginx #如果不能正常啟動,可能是埠占用 
[root@mysql03 nginx]# ps -ef|grep nginx
root 5212 1 0 22:17 ? 00:00:00 nginx: master process ./sbin/nginx
nobody 5213 5212 0 22:17 ? 00:00:00 nginx: worker process
root 5228 2359 0 22:20 pts/0 00:00:00 grep nginx
-- 訪問
瀏覽器輸入: http://10.219.24.26/ #ip換成自己的ip 
看到以下頁面內容,一切正常。
Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

-- 關閉
[root@mysql03 nginx]# ./sbin/nginx -s stop
[root@mysql03 nginx]# ps -ef|grep nginx
root 5241 2359 0 22:25 pts/0 00:00:00 grep nginx

說明: http://www.cnblogs.com/zengkefu/p/5814793.html  部分參考,感謝作者

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31383567/viewspace-2140728/,如需轉載,請註明出處,否則將追究法律責任。

相關文章