1. 程式人生 > >Window下下載並配置Nginx以及SwitchHosts

Window下下載並配置Nginx以及SwitchHosts

一、介紹Nginx

  • 什麼是Nginx

    • Nginx是一款高效能的http 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器。由俄羅斯的程式設計師Igor Sysoev所開發,官方測試nginx能夠支支撐5萬併發連結,並且cpu、記憶體等資源消耗卻非常低,執行非常穩定。

  • 應用場景

    • http伺服器:Nginx是一個http服務可以獨立提供http服務。可以做網頁靜態伺服器(訪問圖片、js、css以及靜態頁面)。
    • 虛擬主機:可以實現在一臺伺服器虛擬出多個網站。例如個人網站使用的虛擬主機。
    • 反向代理,負載均衡(併發高):當網站的訪問量達到一定程度後,單臺伺服器不能滿足使用者的請求時,需要用多臺伺服器叢集可以使用nginx做反向代理。並且多臺伺服器可以平均分擔負載,不會因為某臺伺服器負載高宕機而某臺伺服器閒置的情況。

二、下載Nginx

三、配置檔案

  • 進入解壓好的nginx-1.12.2檔案,進入conf並開啟nginx.conf檔案進行配置
  • 注意下面程式碼中的

    一個service就是一個虛擬主機

  • 
    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
            #監聽埠
            listen       80;
            #域名
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            #定位
            location / {
                #相對目錄相對於安裝目錄,也可以是絕對目錄:D:\nginx\nginx-1.12.2\html
                root   html;
                #歡迎頁
                index  index.html index.htm;
            }
        }
    
        #可以配置其他的Service
        #server {
        #    監聽埠
        #    listen       81;
        #    域名
        #    server_name  localhost;
        #
        #    charset koi8-r;
        #
        #    access_log  logs/host.access.log  main;
        #
        #    定位
        #    location / {
        #       相對目錄相對於安裝目錄,也可以是絕對目錄:D:\nginx\nginx-1.12.2\html81
        #       這裡需要複製一份html並改名為html81
        #       root   html81;
        #       歡迎頁
        #       index  index.html index.htm;
        #   }
        #}
    }
    

 

四、啟動

  • 開啟cmd,檢查配置檔案有沒語法錯誤(注意路徑以及命令:nginx.ext -t)
  • 看到了successful,則表示成功
  • 之後雙擊nginx.exe啟動
  • 檢視工作管理員
  • 之後輸入網址:http://localhost/
  • 注意:因為nginx預設的埠是80,所以這裡不需要寫,如果換成別的埠就需要加上埠,如:localhost:81
  • 出現下圖顯示,則表示啟動成功
  • 這樣Nginx就配置好了

五、下載SwitchHosts

  • 作用:
    • SwitchHosts是一個Hosts快速切換小工具,作用是用來一鍵切換Hosts配置檔案,非常實用。
  • 下載地址:https://github.com/oldj/SwitchHosts/releases
    • 注意:進入軟體後,點選檔案 ->設定 ->可以選擇中文

六、新建本地hosts

  • 點選檔案 ->新建 ->輸入host方案名
  • 這裡需要將按鈕滑動程式設計綠色的,在右邊空白處輸入網址以及域名
  • 我這裡網址是本機的IP,預設埠為80(這裡是Nginx)
  • 這樣就測試成功了