Nginx學習隨筆


題外話

第一份工作中項目中有DBA和運維,所以平時也只關注開發部分,對數據庫和服務器關注比較少,記得那時有用戶反饋網站很慢,老大讓我聯系運維看看是不是服務器的問題,那時也不知道Nginx是個什么東西。這次項目中,開發完之后剛好要做兩個服務器的集群,要用Nginx轉發,蹭這個機會,趕緊學習學習新技能。

 

本地安裝Nginx

1、下載Nginx,當前最新版本nginx-1.9.6.zip,由於只是本地測試,所以下載的是windows版本。

2、解壓,放到指定目錄。

3、配置環境變量,NGINX_PATH並加入到PATH,步奏和配置JDK一樣。好處是執行命令時不用再轉到Nginx的目錄下。

檢查是否安裝成功:

C:\Users\Nginxtest>nginx -v
nginx version: nginx/1.9.6

貌似還是要進入nginx的安裝目錄執行下面的命令。。。。why。。。

 

幾個常用命令

#啟動Nginx
start nginx
#重啟Nginx
nginx -s reload
#停止Nginx
nginx -s stop
#檢查配置是否正確
nginx -t 
#查看Nginx進程
tasklist /fi "imagename eq nginx.exe"

 

啟動Nginx

用start nginx命令啟動

然后在瀏覽器輸入localhost就能訪問了。

修改配置,負載均衡的功能

准備工作:准備了兩個Tomcat,不同的端口號

  localhost:7080

  localhost:9080

 

實踐一:訪問localhost:80時轉發到localhost:9080的tomcat首頁

server {
    #監聽80端口 localhost:80
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        proxy_pass http://localhost:9080;
    }

實踐二、訪問localhost:80時轉發到localhost:9080/springweb/,一定要springweb/

server {
    #監聽80端口 localhost:80
    listen       80;
    server_name  localhost;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    location / {
        proxy_pass http://localhost:9080/springweb/;
    }

實踐三、反向代理,請求轉發

    #真實轉發的地址
    upstream real_path{
        server localhost:9080 weight=1 max_fails=2 fail_timeout=30s;
        server localhost:7080 weight=1 max_fails=2 fail_timeout=30s;
    }
    server {
        #監聽80端口 localhost:80
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            proxy_pass http://real_path;
        }

 

 

附錄一、完整的配置文件說明

參考:https://www.nginx.com/resources/wiki/start/topics/examples/full/

#user  nobody;
#nginx進程數,建議設置為等於CPU總核心數
#設置為n后,tasklist /fi "imagename eq nginx.exe"后n+1個進程
worker_processes  3;
#錯誤日志文件
error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;
#默認最大的並發連接數1024
events {
    worker_connections  1024;
}
#設定Http服務器
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指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設為on
    sendfile        on;
    #防止網絡阻塞
    #tcp_nopush     on;
    #長連接超時時間,單位是秒
    #keepalive_timeout  0;
    keepalive_timeout  65;
    fastcgi_intercept_errors on;
    #開啟gzip壓縮輸出
    #gzip  on;
    
    #負載均衡
    upstream real_path{
        #負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的幾率越大。
        server localhost:9080 weight=1 max_fails=2 fail_timeout=30s;
        server localhost:7080 weight=4 max_fails=2 fail_timeout=30s;
    }

    #虛擬主機的配置
    server {
        #監聽端口
        listen       80;
        #域名可以有多個,用空格隔開
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        #對 "/" 啟用反向代理
        #如果改成location /tomcat/,則訪問http://localhost/tomcat/
        location / {
            proxy_pass http://real_path/;
        }
        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;
        }
}

 

附錄二、nginx的upstream目前支持5種方式的分配

參考:http://blog.chinaunix.net/uid-20662363-id-3049712.html

1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
2、weight
指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
例如:

upstream bakend {
    server 192.168.0.14 weight=10;
    server 192.168.0.15 weight=10;
} 

3、ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
例如:

upstream bakend {
    ip_hash;
    server 192.168.0.14:88;
    server 192.168.0.15:80;
} 

4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。

upstream backend {
    server server1;
    server server2;
    fair;
} 

5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其他的參數,hash_method是使用的hash算法

upstream backend {
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM