正向代理、反向代理linux
(1)位置nginx
正向代理:架設在客戶端和目標主機之間c++
反向代理:架設在服務器端正則表達式
(2)代理對象算法
前者:代理客戶端,服務端不知道實際發起請求的客戶端安全
後者:代理服務端,客戶端不知道實際提供服務的服務端服務器
使用的 Nginx 版本爲 nginx-1.8.0.tar.gz測試
nginx是 C 語言開發,建議在 linux 上運行,使用 Centos6.5 做爲安裝環境。ui
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gccspa
環境,須要安裝 gcc:
yum install gcc-c++
PCRE(Perl Compatible Regular Expressions)是一個 Perl 庫,包括 perl 兼容的正則表
達式庫。nginx的 http 模塊使用 pcre來解析正則表達式,因此須要在 linux 上安裝 pcre庫。
yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre開發的一個二次開發庫。nginx也須要此庫。
zlib庫提供了不少種壓縮和解壓縮的方式, nginx使用zlib對http包的內容進行gzip,
因此須要在 linux 上安裝 zlib 庫。
yum install -y zlib zlib-devel
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和
證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx不只支持 http 協議,還支持 https(即在 ssl 協議上傳輸 http),因此須要在nux 安裝 openssl 庫。
yum install -y openssl openssl-devel
解壓:tar -zxvf nginx-1.8.0.tar.gz
進入到 nginx的根目錄
cd nginx-1.8.0
./configure
參數設置以下:
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注意:上邊將臨時文件目錄指定爲/var/temp/nginx,須要在/var 下建立 temp 及 nginx目錄
編譯:make
編譯安裝 : make install
cd /usr/local/nginx/sbin/
./nginx
注意:執行./nginx 啓動 nginx,這裏能夠-c指定加載的 nginx 配置文件,以下:
./nginx -c /usr/local/nginx/conf/nginx.conf
若是不指定-c,nginx 在啓動時默認加載 conf/nginx.conf 文件,此文件的地址也能夠在編譯安裝 nginx 時指定./configure 的參數(--conf-path= 指 向配置文件(nginx.conf))
方式 1,快速中止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式至關於先查出 nginx進程 id 再使用 kill 命令強制殺掉進程。
方式 2,完整中止(建議使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式中止步驟是待 nginx進程處理任務完畢進行中止。
方式 1,先中止再啓動(建議使用):
對nginx進行重啓至關於先中止nginx再啓動nginx, 即先執行中止命令再執行啓動命令。
以下:
./nginx -s quit
./nginx
方式 2,從新加載配置文件:
當 nginx 的配置文件 nginx.conf修改後,要想讓配置生效須要重啓 nginx,使用-s reload
不用先中止 nginx再啓動 nginx便可將配置信息在 nginx 中生效,以下:
./nginx -s reload
nginx安裝成功,啓動 nginx,便可訪問虛擬機上的 nginx
Nginx默認的是偵聽 80 端口