nginx 是一個開源的高性能 web 服務(wù)器(可能是性能最好的),使用非常廣泛若治,既可以用來部署靜態(tài)資源慨蓝,也可以用來作為反向代理,甚至可以作為負(fù)載均衡服務(wù)器端幼。
安裝和啟動
# 安裝
yum install nginx
# 啟動
service start nginx
# 重新加載配置
nginx -s reload
配置文件
默認(rèn)的配置文件在 /etc/nginx/nginx.conf
礼烈,這個文件是配置文件的入口,一般配置一些全局信息
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
這個配置文件里面一般會有一句 include /etc/nginx/conf.d/*.conf
婆跑,包含各個子服務(wù)的配置信息
靜態(tài)網(wǎng)站
只需要在 /etc/nginx/conf.d
中新增一個文件 example.com.conf
server {
listen 80;
# 這里設(shè)置服務(wù)的名字
server_name example.com;
access_log /var/log/nginx/example.log main;
error_log /var/log/nginx/example.err main;
location / {
# 這里設(shè)置靜態(tài)資源的路徑
root /var/www/example;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
}
-
server_name
: 服務(wù)的名字此熬,用戶通過這個名字訪問服務(wù),不同的服務(wù)使用不同的 server_name 區(qū)分滑进,同一個 nginx 實(shí)例下可部署多個服務(wù) -
root
: 靜態(tài)資源路徑 -
access_log
: 日志輸出路徑犀忱,日志格式通過最后一個參數(shù)指定,這里的 main 為日志格式名扶关,來自于上一個配置文件中的log_format
反向代理
同樣在 /etc/nginx/conf.d
中新增一個文件 proxy.conf
upstream yourservice {
keepalive 32;
server yourserver:<port>;
}
server {
listen 80;
server_name proxy;
access_log /var/log/nginx/proxy.log access;
error_log /var/log/nginx/proxy.err;
location / {
proxy_pass http://yourservice;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header X-Real-IP $http_x_real_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
-
upstream
: 定義一個服務(wù)阴汇,server
里面可以指定多個后端服務(wù)地址,利用 nginx 作負(fù)載均衡 -
proxy_pass
: 反向代理到我們定義的服務(wù)中 -
proxy_set_header
: 請求服務(wù)時設(shè)置一些頭部字段节槐,比如 userAgent 和 客戶端 ip
客戶端 ip 一般設(shè)置在 X-Real-IP 和 X-Forwarded-For
內(nèi)置變量
nginx 的配置文件中提供了大量的內(nèi)置變量
-
$romote_addr
: 客戶端 ip 地址 -
$remote_user
: 客戶端用戶名稱 -
$time_local
: local 格式時間 -
$time_iso8601
: iso8601 格式時間 -
$scheme
: http 協(xié)議 -
$request
: 請求的 url 和 http 協(xié)議 -
$status
: 返回的狀態(tài)碼 -
$body_bytes_sent
: 返回的包體大小 -
$http_referer
: 請求頁面來源搀庶,header["Referer"] -
$http_user_agent
: 瀏覽器信息拐纱,header["User-Agent"] -
$http_x_real_ip
: 用戶真實(shí) ip,header["X-Real-IP"] -
$http_x_forwarded_for
: 代理過程哥倔,header["X-Forwarded-For"]
json 日志
如果有日志分析的需求戳玫,最好使用 json 格式的日志,可以通過 log_format
命令自定義日志格式
http {
log_format access escape=json
'{'
'"@timestamp":"$time_iso8601",'
'"remoteAddr":"$remote_addr",'
'"remoteUser":"$remote_user",'
'"xRealIP":"$http_x_real_ip",'
'"xForwardedFor":"$http_x_forwarded_for",'
'"request":"$request",'
'"status": $status,'
'"bodyBytesSent":"$body_bytes_sent",'
'"resTimeS":"$request_time",'
'"referrer":"$http_referer",'
'"userAgent":"$http_user_agent"'
'}';
}
這個日志格式可以定義在 /etc/nginx/nginx.conf
的 http 字段中未斑,各個 server 就可以直接引用
server {
access_log /var/log/nginx/example.log access;
}
如果希望日期格式顯示為北京時間,需要設(shè)置一下時區(qū)
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" >> /etc/timezone
gzip 壓縮
返回的靜態(tài)資源比較大币绩,帶寬成為瓶頸蜡秽,可以考慮開啟 gzip 壓縮,200k 的文件能壓縮到 幾十k缆镣,效果還挺明顯的芽突,開啟 gzip 的配置也很簡單,直接修改 /etc/nginx/nginx.conf
即可
http {
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_comp_level 8;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript;
gzip_vary on;
gzip_disable "MSIE [1-6]\.";
}
鏈接
- 代碼鏈接: https://github.com/hpifu/docker-nginx
- 官方初學(xué)者教程: http://nginx.org/en/docs/beginners_guide.html
- 官方文檔: http://nginx.org/en/docs/
- nginx 源碼: https://github.com/nginx/nginx
轉(zhuǎn)載請注明出處
本文鏈接:https://tech.hatlonely.com/article/53