1. 程式人生 > >Windows下nginx作為靜態資源伺服器使用小結

Windows下nginx作為靜態資源伺服器使用小結

起因    

        最近在公司接個任務,說用nginx做靜態資源伺服器,給客戶端寫個更新程式,把更新包放nginx伺服器上。當然nginx作為一款輕量級的web伺服器/反向代理伺服器/郵件伺服器,對於這簡單的任務毛毛雨啦大笑,在此總結一下nginx的使用,以備遺忘時翻看。

Nginx使用

        1.下載nginx        直接官網下載:nginx.org

        2.下載完成後是個壓縮包,這裡我把它解壓到E:\nginx(以下記錄均以此目錄為基準)

        3.nginx配置檔案為根目錄下conf\nginx.conf(對應nginx的使用關鍵在於此配置檔案)

        4.啟動nginx    cmd進入nginx根目錄: E:\nginx     start nginx  即啟動nginx

Nginx常用控制檯命令

start nginx        啟動nginx
tasklist /fi "imagename eq nginx.exe"    檢視nginx程序
nginx -s quit      安全關閉
nginx -s stop      強制關閉
nginx -s reload    改變配置檔案時,重啟nginx工作程序,使配置生效
nginx -s reopen    開啟日誌檔案
nginx -v           檢視版本
nginx -h           檢視幫助資訊

特別提醒!!!

        在說Nginx配置檔案之前,特別提醒一下,我踩到的一個坑。

        Nginx在Windows系統下 檢視/修改 conf配置檔案

        千萬不要用記事本開啟!

        千萬不要用記事本開啟!

        千萬不要用記事本開啟!碰都別碰!

因為在Windows系統下,記事本開啟會用 utf-8-Bom 格式開啟檔案,即使你的檔案是 utf-8 的,他也會自以為是的給你加上Bom頭,以他的格式重新編碼儲存,這種格式將破環nginx配置檔案,雖然看上去內容沒有任何改變,但重要的編碼卻被他偷樑換柱了,將導致nginx啟動異常。

這是我有一天手殘用記事本開啟配置後,nginx便無法正常啟動了。檢視配置,內容都沒改變,前兩天還好好的呀。於是檢視logs/error.log 檔案檢視錯誤資訊:

2018/07/05 11:17:59 [emerg] 12268#4676: unknown directive "" in E:\nginx-1.15.0/conf/nginx.conf:5

幾經波折,終查到原因,windows記事本修改了檔案的編碼格式,於是用Notepad++開啟,修改編碼使用UTF-8編碼,檔案另存,故障排除。

有關UTF-8-BOM格式與UTF-8格式更詳細的區別,推薦參看:

Nginx配置檔案

        Nginx主要由conf資料夾下nginx.conf檔案配置,以下為我本地的配置檔案,紅字部分需特別注意,作為靜態資源伺服器的配置部分由黃底紅字標示。

#user  nobody;

# 指定nginx程序數
worker_processes 1;

# 全域性錯誤日誌及PID檔案
#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伺服器,利用它的反向代理功能提供負載均衡支援
http {
    
    #設定mime型別,型別由mime.type檔案定義
    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 指令指定 nginx是否呼叫sendfile 函式(zero copy 方式)來輸出檔案,對於普通應用
    sendfile        on;
    #tcp_nopush     on;
    
    # 連線超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #開啟gzip壓縮,壓縮html
    #gzip  on;
###################################
  # 設定負載均衡的伺服器列表 支援多組的負載均衡,可以配置多個upstream 來服務於不同的Server.
    # nginx 的 upstream 支援幾種方式的分配
    # 1.輪詢(預設)每個請求按時間順序逐一分配到不同的後端伺服器,如果後端伺服器down掉,能自動剔除。
    # 2.weight 指定輪詢機率,weight和訪問比率成正比,用於後端伺服器效能不均的情況。
    # 3.ip_hash 每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端伺服器,可以解決session的問題
    # 4.fair
    # 5.url_hash #Urlhash
    upstream mysvr{
        #weigth引數表示權值,權值越高被分配到的機率越大
        #1.down 表示當前的server暫時不參與負載
        #2.weight 預設為1 weight越大,負載的權重就越大
        #3.backup 其他所有的非backup機器down或者忙的時候,請求backup機器,所以這臺機器的壓力最輕,備用機器

        #server 192.168.1.116    down;
        #server 192.168.1.116    backup;
        #server 192.168.1.142    weight=1;
        server 192.168.1.142    weight=1;
    }

#####################################

    # 配置代理伺服器的地址,即Nginx安裝的伺服器地址、監聽埠、預設地址
    server {
        #1.監聽8099埠
        listen       8099;
        #對於server_name,如果需要將多個域名的請求進行反向代理,可以配置多個server_name來滿足要求
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            # 預設主頁目錄在nginx安裝目錄的html子目錄
            root   html ;
            index  index.html index.htm;
			#proxy_pass http://mysvr; #跟負載均衡伺服器的upstream對應
        }

        #訪問本地E:/source資料夾 訪問路徑為localhost:8099/file/a.png 實際訪問路徑為 E:/source/file/a.png
        location /file/ {
            root E:/source/;
            autoindex on;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

一個簡單的負載均衡示例

# 把www.domain.com均衡到本地不同的埠,也可以改為均衡到不同的地址上。

http{
    upstream  myproject{
      server  127.0.0.1:8000  weight=3;
      server  127.0.0.1:8001  weight=1;
      server  127.0.0.1:8002;
      server  127.0.0.1:8003;
    }
    server{
      listen  80;
      server_name  www.domain.com;
      location / {
          proxy_pass  http://myproject;
      }
     }
}

以上為我使用nginx做靜態資源伺服器的nginx配置,下一篇為此對應的C#更新程式的demo程式。

歡迎大家批評指正奮鬥奮鬥