在linux下使用Nginx構建虛擬機器主機

來源:互聯網
上載者:User

Nginx ("engine x") 是一個高效能的 HTTP 和 反向 Proxy 伺服器,也是一個 IMAP/POP3/SMTP Proxy 伺服器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 網站開發的,它已經在該網站運行超過兩年半了。 Igor 將原始碼以類 BSD 許可證的形式發布。儘管還是測試版,但是,Nginx 已經因為它的穩定性、豐富的功能集、樣本設定檔和低系統資源的消耗而聞名了。

 

1. 擷取Nginx

 

(1) 官方網址 http://nginx.org/en/download.html

 

(2) 官方文檔 http://wiki.nginx.org/

 

(3) 中文文檔 http://wiki.nginx.org/NginxChs

 

目前最新版為 當前穩定版: Nginx 1.0.2 (2011-05-10)

 

2. 安裝Nginx


# wget http://nginx.org/download/nginx-1.0.2.tar.gz# tar zxvf nginx-1.0.2.tar.gz# cd nginx-1.0.2# ./configure# make# make install
預設會安裝到 /usr/local/nginx 目錄下
3. Nginx 常用命令
(1) 啟動  /usr/local/nginx/sbin/nginx 
(2) 停止  /usr/local/nginx/sbin/nginx -s stop
(3)重新載入設定檔(熱重啟)  /usr/local/nginx/sbin/nginx -s reload
(4)設定檔進行檢查  /usr/local/nginx/sbin/nginx -t
(5)  /usr/local/nginx/sbin/nginx  -p 路徑 ,重新設定ngix的啟動目錄 (default: /usr/local/nginx/)
      如: /usr/local/nginx/sbin/nginx  -p  /home/nginx
(6) /usr/local/nginx/sbin/nginx -c filename , 重新設定設定檔 (default: conf/nginx.conf) 
     如: /usr/local/nginx/sbin/nginx  -c  /home/nginx/nginx.conf
(7) 設定開機啟動服務
     在/etc/rc.local檔案中加入nginx啟動命令,如  /usr/local/nginx/sbin/nginx 
4. 配置虛擬機器主機
(1) 開啟設定檔usr/local/nginx/conf/nginx.conf,可以看到http中包含多個server,注釋掉配置中預設的server,
在http中加上
server {        listen       80;        charset utf8;        server_name  www.XXXXXX.com alias XXXXX.com;        location / {          proxy_pass              http://117.0.0.138:8881;      proxy_set_header        X-Real-IP $remote_addr;      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;      proxy_set_header        Host $http_host;        }    }   server {        listen       80;        charset utf8;        server_name  abc.com;        location / {            proxy_pass              http://117.0.0.138:10010;        proxy_set_header        X-Real-IP $remote_addr;        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_set_header        Host $http_host;        }    }

 那麼兩個虛擬機器主機就配置好了。一個是XXXXXX.com,一個是abc.com。

 

5.  給虛擬機器設定緩衝,相當於 Squid 服務。

 

(1) 緩衝不適合即時性很高的系統。

 

(2) 配置如下:

 

proxy_cache_path  /usr/local/nginx/cache  levels=1:2    keys_zone=STATIC:10m

                                         inactive=24h  max_size=1g;

server {        listen       80;        charset utf8;        server_name  www.XXXXXX.com alias XXXXX.com;        location / {          proxy_pass              http://117.0.0.138:8881;      proxy_set_header        X-Real-IP $remote_addr;      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;      proxy_set_header        Host $http_host;
  proxy_cache            STATIC; #緩衝的名字 對應上邊的  keys_zone=STATIC:10m          proxy_cache_valid      200  1d;          proxy_cache_use_stale  error timeout invalid_header updating                                   http_500 http_502 http_503 http_504;        }    }

 

 

6.  啟動nginx

 

(1) 執行 /usr/local/nginx/sbin/nginx   即可啟動nginx

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.