nginx 入門教程

nginx 是一個開源的高性能 web 服務器(多是性能最好的),使用很是普遍,既能夠用來部署靜態資源,也能夠用來做爲反向代理,甚至能夠做爲負載均衡服務器。javascript

安裝和啓動

# 安裝
yum install nginx

# 啓動
service start nginx

# 從新加載配置
nginx -s reload

配置文件

默認的配置文件在 /etc/nginx/nginx.conf,這個文件是配置文件的入口,通常配置一些全局信息css

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,包含各個子服務的配置信息html

靜態網站

只須要在 /etc/nginx/conf.d 中新增一個文件 example.com.confjava

server {
    listen 80;
    # 這裏設置服務的名字
    server_name example.com;

    access_log /var/log/nginx/example.log main;
    error_log /var/log/nginx/example.err main;

    location / {
        # 這裏設置靜態資源的路徑
        root /var/www/example;
        try_files $uri $uri/ /index.html;
        index index.html index.htm;
    }
}
  • server_name: 服務的名字,用戶經過這個名字訪問服務,不一樣的服務使用不一樣的 server_name 區分,同一個 nginx 實例下可部署多個服務
  • root: 靜態資源路徑
  • access_log: 日誌輸出路徑,日誌格式經過最後一個參數指定,這裏的 main 爲日誌格式名,來自於上一個配置文件中的 log_format

反向代理

一樣在 /etc/nginx/conf.d 中新增一個文件 proxy.confnginx

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: 定義一個服務,server 裏面能夠指定多個後端服務地址,利用 nginx 做負載均衡
  • proxy_pass: 反向代理到咱們定義的服務中
  • proxy_set_header: 請求服務時設置一些頭部字段,好比 userAgent 和 客戶端 ip

客戶端 ip 通常設置在 X-Real-IP 和 X-Forwarded-Forgit

內置變量

nginx 的配置文件中提供了大量的內置變量github

  • $romote_addr: 客戶端 ip 地址
  • $remote_user: 客戶端用戶名稱
  • $time_local: local 格式時間
  • $time_iso8601: iso8601 格式時間
  • $scheme: http 協議
  • $request: 請求的 url 和 http 協議
  • $status: 返回的狀態碼
  • $body_bytes_sent: 返回的包體大小
  • $http_referer: 請求頁面來源,header["Referer"]
  • $http_user_agent: 瀏覽器信息,header["User-Agent"]
  • $http_x_real_ip: 用戶真實 ip,header["X-Real-IP"]
  • $http_x_forwarded_for: 代理過程,header["X-Forwarded-For"]

json 日誌

若是有日誌分析的需求,最好使用 json 格式的日誌,能夠經過 log_format 命令自定義日誌格式web

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 就能夠直接引用docker

server {
    access_log /var/log/nginx/example.log access;
}

若是但願日期格式顯示爲北京時間,須要設置一下時區json

ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
echo "Asia/Shanghai" >> /etc/timezone

gzip 壓縮

返回的靜態資源比較大,帶寬成爲瓶頸,能夠考慮開啓 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://tech.hatlonely.com/edit/53
相關文章
相關標籤/搜索