nginx反向代理 負載均衡 動靜分離

Nginx Tomcat CSS 技術 安卓網hiapk 2017-05-20
nginx反向代理 負載均衡 動靜分離

nginx安裝

Nginx 是什麼

Nginx 是一款輕量級的web服務器,也是一款輕量級的反向服務器。

Nginx主要作用

  • 作為反向代理服務器。
  • 作為郵件代理服務器。
  • 實現前端動靜分離。

Nginx 特點

高穩定, 高性能,資源佔用少,功能豐富,模塊化結構,支持熱部署。

Nginx 配置文件

  • /conf/nginx.conf: 應用程序的基本配置文件。
  • /mime.types: MIME類型關聯的擴展文件。
  • fastcgi.conf: 與fastcgi 相關的配置。
  • proxy.conf: 與proxy 相關的配置。
  • sites.conf: 配置Nginx提供的網站,包括虛擬主機·

    Nginx 的進程結構

    啟動Nginx 的時候,會啟動一個Master進程,這個進程不處理任何客戶端請求,主要用來產生worker進程,一個worker 進程用來處理一個request。

進程

Nginx 文檔

英文文檔中文文檔

指令格式

Syntax: client_body_timeout time;Default: client_body_timeout 60s;Context: http, server, location

  • 第一行語法格式。
  • 第二行默認值。
  • 第三行 指令可以放在的位置。

Nginx模塊分為

核心模塊,事件模塊,標準http模塊,可選http模塊,郵件模塊,第三方模塊和補丁。

  • 核心模塊:基本功能和指令,如進程管理和安全。
  • 事件模塊:在Nginx內配置網絡使用能力。
  • 配置模塊:提供包含機制。

nginx.conf 文檔結

nginx反向代理 負載均衡 動靜分離

文檔結構

核心模塊

指令文檔

1,error_log

日誌 級別debug, info, notice, warn, error, crit, alert, or emerg. 可以設置的位置main, http, mail, stream, server, location error_log logs/error.log error;

2, include

引入文件 可以在任意存放位置 include mime.types;

3, pid

nginx的pid 存放位置 只能在main 位置配置 pid nginx.pid;

4, user

所在用戶組

5, worker_processes

定義進程數 Syntax:    worker_processes number | auto; Default:     worker_processes 1; Context:    main

Location 區段配置

Location 基本語法

location [=|~*|^~|@] pattern{....}

1,沒有修飾符 表示:必須以指定模式開始
server{     server_name xxx.com     location /abc{         ....     }     ..... }  以下url 都可以匹配 http://xxx.com/abc http://xxx.com/abc?p1=1&p2=11 http://xxx.com/abc/ http://xxx.com/abcefg

2, = 表示:必須與指定的模式精確匹配
server{     server_name xxx.com     location = /abc{         ....     }     ..... }  以下url 都可以匹配 http://xxx.com/abc http://xxx.com/abc?p1=1&p2=11  以下url 不能匹配 http://xxx.com/abc/ http://xxx.com/abcefg

3, ~ 表示: 指定的正則表達式要區分大小寫
server{     server_name xxx.com     location ~ ^/abc${         ....     }     ..... }  以下url 都可以匹配 http://xxx.com/abc http://xxx.com/abc?p1=1&p2=11  以下url 不能匹配 http://xxx.com/abc/ http://xxx.com/abcefg http://xxx.com/ABC

4, ~* 表示:指定正則表達式不區分大小寫
server{     server_name xxx.com     location ~* ^/abc${         ....     }     ..... }  以下url 都可以匹配 http://xxx.com/abc http://xxx.com/abc?p1=1&p2=11 http://xxx.com/ABC  以下url 不能匹配 http://xxx.com/abc/ http://xxx.com/abcefg

5, ^~ 類似於無修飾符的行為,也是以指定模式開始,不同的是,如果模式匹配,那麼

就停止搜索其它模式了。

6, @ 定義命名location區段,這些區段用戶不能訪問,只可以由內部產生的請求來訪問

如error_page

查找順序和優先級
  • 帶有“=” 的精確匹配優先。
  • 沒有修飾符的精確匹配。
  • 正則表達式按照他們在配置文件中定義順序。
  • 帶有“^~” 修飾符的,開頭匹配。
  • 帶有“~” 或“~*” 修飾符的,如果正則表達式與uri匹配。
  • 沒有修飾符的,如果指定字符串與uri開頭匹配

負載均衡 反向代理

在http 模塊設置  upstream ceshi {     server  127.0.0.1:8484  weight=5;      server  127.0.0.1:8888  weight=5; } location /root {     #http:// + “upstream”     proxy_pass http://ceshi/; }

動靜分離

location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff)$ {     #所有靜態文件直接讀取硬盤     root  /Users/lihao/Desktop/work/tomcat/tomcat/webapps/ROOT/;     expires 30d; #緩存30天 }  若是訪問不到 用chmod 修改文件夾權限。

相關推薦

推薦中...