windows下使用nginx

本文介紹如何在windows下使用nginx

起步

下載安裝

下載 http://nginx.org/download/

進入根目錄運行 
start nginx

若是報錯或者沒法啓動應該是80端口被佔用,設置nginx.conf文件將80端口換成其餘端口,我換成了8085

訪問 http://localhost:8085/,能夠看到nginx的歡迎界面

將nginx安裝成windows服務

步驟1,下載winsw工具 http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/

步驟2,我下載的winsw是winsw-2.1.2-bin.exe,將其放置於nginx根目錄更名爲nginxsvc.exe

步驟3,建立nginxsvc.xml置於nginx根目錄,寫入以下配置(注:國內外網站的配置文件都有問題,寫以下配置文件直接跑通)
    <service>
        <id>nginx</id>
        <name>nginx</name>
        <description>nginx</description>
        <executable>C:\nginx\nginx.exe</executable>
        <logpath>C:\nginx\logs</logpath>
        <logmode>roll</logmode>
        <depend></depend>
        <startargument>-pc:\nginx</startargument>
        <stopexecutable>c:\nginx\nginx.exe</stopexecutable>
        <stopargument>-s</stopargument>
        <stopargument>stop</stopargument>
    </service>
步驟4,運行 nginxsvc.exe install 安裝服務
步驟5,到服務界面啓動服務
步驟6,修改服務登陸用戶,點擊服務->屬性,將服務登陸用戶修改成當前電腦用戶  
    若是不這麼作,nginx的命令你都無法執行

經常使用命令

nginx -s stop                           暴力的中止nginx
nginx -s quit                           優雅的中止nginx 
nginx -s reload                         從新加載conf配置
nginx -s reopen                         從新打開配置文件
taskkill /f /pid  28544                 結束進程 
tasklist /fi "imagename eq nginx.exe"   查看nginx進程
nginx -c ./conf/nginx.conf              指定配置文件開啓nginx

構建服務

靜態服務

進入nginx.conf添加以下配置

server {
    listen 10000;
    server_name  localhost;
    location /sjl/ {
        root   data;
        index  index.html index.htm;
    }
}

此配置告知nginx,當有請求 http://localhost:10000/sjl/

訪問nginx根目錄 data/sjl/index.html

代理服務器

server {
    listen 10000;
    server_name  localhost;
    location /sjl/ {
        root   data;
        index  index.html index.htm;
    }
}

server {
    listen 10001;
    root   data;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:10000/sjl/;
    }
}

監聽 http://localhost:10001 

將訪問http://localhost:10001代理到http://localhost:10000/sjl/

也就是說你訪問http://localhost:10001其實是訪問http://localhost:10000/sjl/

http配置文件轉移

爲了配置清晰明朗,在你的nginx.conf的http模塊中

http {
    ...
    include       selfconf/my.conf;
    ...
}

在nginx.conf同級目錄下建立selfconf/my.conf文件

在my.conf中直接填寫server配置便可

負載均衡

負載均衡配置

我沒有那麼多電腦,因此就用nginx作了多個server的模擬

// 均分請求到下面三個模擬服務器
upstream localhost {
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003 backup;  // 這個是備用服務器,當前面兩個搞不定了,再分配到此處
}

server {
    listen 10000;
    server_name  127.0.0.1;
    location / {
        proxy_pass http://localhost;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

// 下面模擬三個服務器
server {
    listen 10001;
    server_name  127.0.0.1;
    location / {
        root   data/sji/;
        index  index.html index.htm;
    }
}

server {
    listen 10002;
    server_name  127.0.0.1;
    location / {
        root   data/sjl/;
        index  index.html index.htm;
    }
}

server {
    listen 10003;
    server_name  127.0.0.1;
    location / {
        root   data/sjm/;
        index  index.html index.htm;
    }
}

負載均衡方法

第一種:Round Robin,默認方式請求權重均分

    upstream localhost {
        server 127.0.0.1:10001 weight=5; // 權重爲5
        server 127.0.0.1:10002 max_fails=3 fail_timeout=30s; // 設置最大重連次數爲3次,和最大不可用時長30s
        server 127.0.0.1:10003 backup; // 備用服務器
    }
第二種:Least Connections,將請求分配給鏈接數最少的服務器

    upstream localhost {
        least_conn;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003 backup;
    }
第三種,IP Hash,相同的ip訪問都會被分配到同一個服務器

    upstream localhost {
        ip_hash;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003 down; // 此服務器不參與分配
    }
第四種,Generic Hash,能夠自定義檢測key

    key相同則負載到同一臺服務器上

    upstream localhost {
        hash $request_uri consistent;
        server 127.0.0.1:10001;
        server 127.0.0.1:10002;
        server 127.0.0.1:10003;
    }

    $request_uri    表示的是http url後邊的uri
    $args           查詢參數
    $remote_addr    客戶端的IP
    $remote_port    客戶端的端口
    ... 相似的還有不少

正式線上的負載均衡配置

// ipAdress 這是你的服務器IP地址

// proxyAdress 這是你的代理服務器IP地址

// 假設你的後臺api接口爲 ipAdress:9000/test 和 ipAdress:9001/test 能夠使用以下方式進行負載均衡

// 經過你的代理服務器 proxyAdress:10000/test 便可負載到上面兩個服務器對應的test api

upstream ipAdress {
    server ipAdress:9000;
    server ipAdress:9001;
}

server {
    listen 10000;
    
    server_name  proxyAdress;

    location /test/ {
        proxy_pass http://ipAdress;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}
相關文章
相關標籤/搜索