1. 程式人生 > >遠端伺服器上nginx安轉與基本操作(ip反向代理轉發)

遠端伺服器上nginx安轉與基本操作(ip反向代理轉發)

因為業務系統需求,需要對web服務作nginx代理,在不斷的嘗試過程中,簡單總結了一下常見的nginx代理配置。

下載安裝

下載原始碼

這是1.2.8版本其他版本自己下載。

安裝

#解壓
tar -zxvf nginx-1.2.8.tar.gz
#進入目錄
cd nginx-1.2.8  
配置安轉目錄
./configure --prefix=/usr/local/nginx --with-stream
編譯
make
make install

——————————————-分界線開始—————————————-
最近配置的時候報錯缺少依賴如下:

./configure: error: the
HTTP rewrite module requires the PCRE library. You can either disable the module by using --without-http_rewrite_module option, or install the PCRE library into the system, or build the PCRE library statically from the source with nginx by using --with-pcre=<path> option. 解決: $ apt-get update $ apt-get
install libpcre3 libpcre3-dev # wget http://sourceforge.net/projects/pcre/files/pcre/8.32/pcre-8.32.tar.gz/download # tar -xzvf pcre-8.32.tar.gz # cd pcre-8.32 # ./configure --prefix=/usr/local/pcre # make && make install ./configure: error: the HTTP gzip module requires the zlib library. You can either disable the
module by using --without-http_gzip_module option, or install the zlib library into the system, or build the zlib library statically from the source with nginx by using --with-zlib=<path> option. 解決: $ sudo apt-get install zlib1g-dev

最暴力的解決(可能執行會報錯):

$ ./configure --prefix=/usr/local/nginx --without-http_gzip_module 

——————————————-分界線結束—————————————-
至此安裝完成

驗證

啟動:

/usr/local/nginx/sbin/nginx

開啟瀏覽器:

輸入安裝nginx的ip

出現:
Welcome to nginx!

轉發配置

vi /usr/local/nginx/conf/nginx.conf

如圖所示

注意兩個關鍵詞:location 和proxy_pass
location:就是字尾,proxy_pass:就是轉發到的相應的ip及其埠。
如下:

location /{
    proxy_pass http://localhost:8080/index.html
}
#這句話就是說把所有的localhost請求轉發到http://localhost:8080/index.html

location /test{
    proxy_pass http://localhost:8080/test/index.html
}
#這句話就是說把所有的localhost/test請求轉發到http://localhost:8080/test/index.html
#注意:不要location 不要寫成 /test/ 因為location的pattern識別的路徑作為絕對路徑。

是不是很簡單,對很簡單,不過這是nginx最基礎的用法,if you want to more , 進入官網咖

———分界線——-
如果需要使用https相關服務,–with-http_ssl_module

sudo apt-get install openssl libssl-dev