概念
負載均衡服務器:
正向代理服務器:
反向代理服務器:
Nginx組成
- Nginx二進制可執(zhí)行文件
- Nginx.conf配置文件
- access.log訪問日志
- error.log錯誤日志
安裝
步驟
-
下載
Configure
編譯
安裝
## 安裝依賴包
yum install pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel
## 開始
cd /usr/local/src
wget http://nginx.org/download/nginx-1.16.0.tar.gz #下載
tar -xvf nginx-1.16.0.tar.gz #解壓
cd nginx-1.16.0
[圖片上傳失敗...(image-a1d2fa-1563935544116)]
./configure
make
make install
Windows
基本使用
下載
-
解壓
[圖片上傳失敗...(image-9fe26c-1563935544117)]
-
測試
start nginx http://127.0.0.1:80
一些命令
幫助 -? -h
使用指定的配置文件 -c
指定配置指令 -g
指定運行目錄 -p
發(fā)送信號 -s
- 立刻停止服務
stop
- 優(yōu)雅停止服務
quit
- 重載配置文件
reload
- 重新開始記錄日志文件
reopen
nginx -v #查看nginx的版本
start nginx #啟動Nginx
nginx -s reload #重新加載配置文件
nginx -s stop #快速停止
nginx -s quit #完全停止
nginx -t #驗證配置是否正確
配置文件
查看默認的配置文件
#user nobody;
# Nginx進程數
worker_processes 1;
# 全局錯誤日志定義類型 [debug|info|notice|warn|error|crit]
#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 {
include mime.types; # 文件擴展名與文件類型映射表
default_type application/octet-stream; # 默認文件類型
#charset utf-8; # 默認編碼
#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; # 開啟高效文件傳輸模式 sendfile指令指定nginx是否調用sendfile函數來輸出文件酷麦,對于普通應用設為 on矿卑,如果用來進行下載等應用磁盤IO重負載應用,可設置為off沃饶,以平衡磁盤與網絡I/O處理速度母廷,降低系統(tǒng)的負載。
#tcp_nopush on; #防止網絡阻塞
keepalive_timeout 65; # 長連接超時時間 單位為s
gzip on;
server {
listen 80; #監(jiān)聽端口
server_name localhost; # 域名列表 中間用空格分隔
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
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;
# }
#}
}
配置文件由指令與指令塊構成
- 每條指令以分號結尾
- { }將多條指令組織在一起
-
include
允許組合多個配置文件以提升可維護性 -
$
使用變量 -
四大塊
http
server
location
upstream
location / {
root html; # 訪問域名根目錄
index index.html index.htm; #設置訪問主頁
}
root定義了工作空間
index決定那個優(yōu)先顯示
gzip模塊設置
gzip on; #開啟gzip壓縮輸出
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩沖區(qū)
gzip_http_version 1.0; #壓縮版本(默認1.1糊肤,前端如果是squid2.5請使用1.0)
gzip_comp_level 2; #壓縮等級
gzip_types text/plain application/x-javascript text/css application/xml; #壓縮類型琴昆,默認就已經包含text/html,所以下面就不用再寫了馆揉,寫上去也不會有問題业舍,但是會有一個warn。
gzip_vary on;
反向代理
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #后端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
proxy_set_header Host $host;
client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數
client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數升酣,
proxy_connect_timeout 90; #nginx跟后端服務器連接超時時間(代理連接超時)
proxy_send_timeout 90; #后端服務器數據回傳時間(代理發(fā)送超時)
proxy_read_timeout 90; #連接成功后舷暮,后端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網頁平均在32k以下的設置
proxy_busy_buffers_size 64k; #高負荷下緩沖大胸选(proxy_buffers*2)
proxy_temp_file_write_size 64k;
日志訪問
日志參數說明
參數 | 說明 |
---|---|
$remote_addr |
客戶端IP(公網IP) |
$http_x_forwarded_for |
代理服務器的IP |
$time_local |
服務器本地時間 |
$host |
訪問主機名 |
$request_uri |
訪問的url地址 |
$status |
狀態(tài)碼 |
$http_referer |
|
$http_user_agent |
案例
搭建一個靜態(tài)資源Web服務器
配置
server{
listen 9804;
server_name localhost; #指定域名
index index.html index.php; #優(yōu)先級
access_log logs/book.access.log main;
location / {
alias Book;
autoindex on;
index index.html;
set $limit_rate 1k; #設置請求大小
}
}
搭建具有緩存功能的的反向代理服務器
開啟上游服務
一個SpringBoot應用
java -jar .\app-0.0.1-SNAPSHOT.jar
[圖片上傳失敗...(image-6fdae2-1563935544117)]
127.0.0.1:8080
即可看到
配置nginx.conf
server{
listen 9805;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
}
}
這樣當我們訪問127.0.0.1:9805
即看到127.0.0.1:8080
頁面
[圖片上傳失敗...(image-c22158-1563935544117)]
Nginx所代表的角色是負載均衡服務器或者反向代理服務器下面,所有的請求首先到Nginx上,再由Nginx根據配置好的轉發(fā)規(guī)則巢墅,將客戶端發(fā)來的請求轉發(fā)到某一個Tomcat上诸狭。