1. 程式人生 > 實用技巧 >nginx安裝、配置與使用

nginx安裝、配置與使用

#下載地址

http://nginx.org/en/download.html

選擇Stable version下載

#安裝

解壓即可

#使用

1)作為檔案伺服器

修改conf資料夾下的nginx.conf檔案,在server內新增以下配置

location /visit {
     alias D:/FTP/dsm;
     autoindex on;
     autoindex_exact_size off;
     autoindex_localtime on;
}

其中:

alias 當客戶端請求/visit/file.ext 的時候,Nginx把請求對映為D:/FTP/dsm/file.ext;與root的區別是:root則把請求對映為D:/FTP/dsm/visit/file.ext 。

autoindex 預設是不允許列出整個目錄的,如需此功能,則設定為on。

autoindex_exact_size  預設為on,顯示出檔案的確切大小,單位是bytes。

autoindex_localtime 預設為off,顯示的檔案時間為GMT時間,改為on後,顯示的檔案時間為檔案的伺服器時間。

2)作為負載均衡

Nginx提供的負載均衡策略有2種:內建策略和擴充套件策略。內建策略為輪詢,加權輪詢,Ip hash。擴充套件策略,就天馬行空,只有你想不到的沒有他做不到的啦,你可以參照所有的負載均衡演算法,給他一一找出來做下實現。

修改conf資料夾下的nginx.conf檔案,在http內新增以下配置

upstream test{
    ip_hash;
    server 192.168.1.250:15005;
    server 192.168.1.115:8080;
}

在server內新增以下配置

location / {
   proxy_pass http://test;
}

#啟動或關閉nginx

1)啟動

在bin目錄下執行

start nginx

2)關閉

nginx -s stop

3)重啟

nginx -s reopen

4)重新載入配置

nginx -s reload