nginx配置靜態web伺服器

來源:互聯網
上載者:User

標籤:nginx   web伺服器   

配置靜態web,需要實現一個虛擬機器主機。

step1: 準備工作

            1  查看你的網卡地址(我的 192.168.223.135)

                 #ifconfig

                 我們這裡建立兩個web伺服器,所以增加一個網卡地址。

#ifconfig eth0:0  192.168.223.145

            2 建立兩個網站目錄

                #mkdir  /var/tmp/website1

                #mkdir  /var/tmp/website2

            3 建立兩個日誌目錄

               #mkdir  /var/tmp/log/website1

               #mkdir  /var/tmp/log/website2

            4 建立兩個測試頁

              #echo "this is website1" > /var/tmp/website1/index.html

              #echo "this is website2" > /var/tmp/website2/index.html

step2: 修改設定檔,並增加一個server節點

            1 找到你的nginx的安裝目錄,修改設定檔

             #vim  /usr/local/nginx/conf/nginx.conf

             修改設定檔中的server節點

             

server {        listen 192.168.223.135:80;        server_name localhost;        #charset koi8-r;        access_log /var/tmp/log/website1/access.log;        error_log /var/tmp/log/website1/error.log;        location / {            root /var/tmp/website1;            index index.html index.htm;        }        error_page 404 /404.html;        # redirect server error pages to the static page /50x.html        #        error_page 500 502 503 504 /50x.html;        location = /50x.html {               root html;        }}

              添加一個server節點

server {   listen 192.168.223.145:80;  server_name localhost;  #charset koi8-r;  access_log /var/tmp/log/website2/access.log;  error_log /var/tmp/log/website2/error.log;  location / {  root /var/tmp/website2;  index index.html index.htm;  }  error_page 404 /404.html;  # redirect server error pages to the static page /50x.html  #  error_page 500 502 503 504 /50x.html;  location = /50x.html {  root html;  }}
                2 啟動nginx

                #/usr/local/nginx/sbin/nginx

                用戶端 開啟網頁,訪問網站 http://192.168.223.135    http://192.168.223.145 試試


如果你覺得這樣輸入ip地址來訪問過於醜陋,還可以修改成基於主機頭的,例如:www.xxxxxxx.com形式

step 1: 修改server_name

          我們只需要修改上述兩個server節點中的server_name即可。

           第一個修改為:server_name  www.website1.com

           第二個修改為:server_name  www.website2.com

step2 :修改本機hosts檔案

           在hosts檔案中加入

           192.168.223.135   www.website1.com

           192.168.223.145   www.website2.com

            #echo "192.168.223.135   www.website1.com"  >> /etc/hosts

            #echo "192.168.223.145   www.website2.com"  >> /etc/hosts

           有的機器的hosts檔案可能為唯讀檔案,修改需要root許可權,你可以這樣子幹

           為其添加一個寫入權限:

           #chmod  a+w  /etc/hosts

           然後再執行上面的添加指令。

step3: 重啟nginx

           #pkill nginx

           #/usr/local/nginx/sbin/nginx

            然後再客戶機開啟瀏覽器,輸入www.website1.com   www.website2.com 試試。






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.