Nginx 目錄
$ cd /etc/nginx
$ ls -l
total 60
drwx------ 2 ubuntu ubuntu 4096 Jun 16 09:27 cert ## ssl證書目錄
drwxr-xr-x 2 root root 4096 Jul 12 2017 conf.d
-rw-r--r-- 1 root root 1077 Feb 11 2017 fastcgi.conf
-rw-r--r-- 1 root root 1007 Feb 11 2017 fastcgi_params
-rw-r--r-- 1 root root 2837 Feb 11 2017 koi-utf
-rw-r--r-- 1 root root 2223 Feb 11 2017 koi-win
-rw-r--r-- 1 root root 3957 Feb 11 2017 mime.types
-rw-r--r-- 1 root root 1501 Aug 31 07:42 nginx.conf ## 配置文件
-rw-r--r-- 1 root root 180 Feb 11 2017 proxy_params
-rw-r--r-- 1 root root 636 Feb 11 2017 scgi_params
drwxr-xr-x 2 root root 4096 Aug 31 09:42 sites-available ## 虛擬主機(jī)配置代理目錄
drwxr-xr-x 2 root root 4096 Jun 15 06:39 sites-enabled ## 啟動配置代理目錄
drwxr-xr-x 2 root root 4096 Jun 4 06:03 snippets
-rw-r--r-- 1 root root 664 Feb 11 2017 uwsgi_params
-rw-r--r-- 1 root root 3071 Feb 11 2017 win-utf
nginx 配置文件的關(guān)系:
常用命令
## 查看 Nginx 程序文件目錄:/usr/sbin/nginx
$ ps -ef | grep nginx
## 查看 nginx.conf 配置文件目錄:/etc/nginx/nginx.conf
$ nginx -t
$ vim /etc/nginx/nginx.conf
## 配置文件目錄:/etc/nginx
## 虛擬主機(jī)配置文件目錄:/etc/nginx/sites-available/
## 虛擬主機(jī)文件夾目錄:/var/www/漫雷,詳情可在 /etc/nginx/sites-available/ 中配置
## 默認(rèn)網(wǎng)頁文件目錄:/usr/share/nginx/html
## 測試配置文件秃励,只檢查配置文件是否存在語法錯誤
$ nginx -t -c <path-to-nginx.conf>
$ sudo nginx -t -c /etc/nginx/nginx.conf
## 啟動 Nginx 服務(wù)
$ nginx 安裝目錄 -c <path-to-nginx.conf>
$ sudo /etc/init.d/nginx start
## 停止 Nginx 服務(wù)
$ sudo /usr/sbin/nginx -s stop
## 重啟 Nginx
$ sudo /usr/sbin/nginx -s reload # 0.8 版本之后的方法
$ kill -HUP pid # 向 master 進(jìn)程發(fā)送信號從容地重啟 Nginx请琳,即服務(wù)不中斷
$ sudo service nginx start
$ sudo service nginx stop
$ sudo service nginx restart
官方配置說明:
?? 推薦一個 HTTPS 自動化配置工具:Mozilla SSL Configuration Generator
Nginx 配置文件:/etc/nginx/nginx.conf
-
/etc/nginx/nginx.conf
配置文件脸狸,在虛擬主機(jī)部分包含了這兩個文件:## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*;
配置代理目錄: /etc/nginx/sites-available/
- sites-available 目錄中存放著具體的 Server 配置文件余舶。
- 默認(rèn)配置文件:
/etc/nginx/sites-available/default
文件詳細(xì)配置了默認(rèn)的虛擬主機(jī)目錄root /var/www/html
寞肖,監(jiān)聽的端口是80恬试。
啟動配置代理目錄:/etc/nginx/sites-enabled/
sites-enabled 目錄中存放的是鏈接文件笋婿,每個鏈接文件都指向 sites-available 目錄中的配置文件嵌屎,表示需要讓 Nginx 啟用哪些配置代理文件推正。
-
在 sites-enabled 下創(chuàng)建 symbol link 鏈接 sites-available 下的配置文件就可以啟用代理配置。
$ sudo ln -s /etc/nginx/sites-available/availableFileName linkFileName
默認(rèn)啟用的服務(wù):
/etc/nginx/sites-enabled/
中的 default 軟鏈接指向/etc/nginx/sites-available/default
宝惰,刪除該鏈接文件植榕,就會關(guān)閉該配置代理。
nginx.conf 配置文件詳解
Nginx 配置文件路徑:/etc/nginx/nginx.conf
##
# 全局配置
##
user www-data; ## 配置 worker 進(jìn)程的用戶和組
worker_processes auto; ## 配置 worker 進(jìn)程啟動的數(shù)量尼夺,建議配置為 CPU 核心數(shù)
error_log logs/error.log; ## 全局錯誤日志
pid /run/nginx.pid; ## 設(shè)置記錄主進(jìn)程 ID 的文件
worker_rlimit_nofile 8192; ## 配置一個工作進(jìn)程能夠接受并發(fā)連接的最大數(shù)
##
# 工作模式及連接數(shù)上限
##
events {
# epoll 是多路復(fù)用 IO(I/O Multiplexing)中的一種方式尊残,
# 僅用于 Linux 2.6 以上內(nèi)核,可以大大提高 Nginx 性能
use epoll
# 單個后臺 worker process 進(jìn)程的最大并發(fā)鏈接數(shù)
# 并發(fā)總數(shù) max_clients = worker_professes * worker_connections
worker_connections 4096; ## Defaule: 1024
# multi_accept on; ## 指明 worker 進(jìn)程立刻接受新的連接
}
##
# http 模塊
##
http {
##
# Basic Settings
##
#sendfile 指令指定 nginx 是否調(diào)用 sendfile 函數(shù)(zero copy 方式)來輸出文件淤堵,
#對于普通應(yīng)用寝衫,必須設(shè)為 on,
#如果用來進(jìn)行下載等應(yīng)用磁盤 IO 重負(fù)載應(yīng)用,可設(shè)置為 off拐邪,
#以平衡磁盤與網(wǎng)絡(luò) I/O 處理速度慰毅,降低系統(tǒng)的 uptime.
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65; ## 連接超時時間
types_hash_max_size 2048; ## 指定散列類型表的最大大小
# server_tokens off;
# server_names_hash_bucket_size 64; # this seems to be required for some vhosts
# server_name_in_redirect off;
include /etc/nginx/mime.types; ## 設(shè)定 mine 類型
default_type application/octet-stream;
# 設(shè)定請求緩沖
client_header_buffer_size 128k; # 指定客戶端請求頭緩存大小,當(dāng)請求頭大于 1KB 時會用到該項(xiàng)
large_client_header_buffers 4 128k; # 最大數(shù)量和最大客戶端請求頭的大小
##
# SSL Settings
##
# 啟用所有協(xié)議扎阶,禁用已廢棄的不安全的SSL 2 和SSL 3
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
# 讓服務(wù)器選擇要使用的算法套件
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log; ## 訪問日志
error_log /var/log/nginx/error.log; ## 錯誤日志
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf; # 這個文件夾默認(rèn)是空的
include /etc/nginx/sites-enabled/*; # 開啟的 Server 服務(wù)配置
}
##
# mail 模塊
##
mail {
# See sample authentication script at:
# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# auth_http localhost/auth.php;
# pop3_capabilities "TOP" "USER";
# imap_capabilities "IMAP4rev1" "UIDPLUS";
server {
listen localhost:110;
protocol pop3;
proxy on;
}
server {
listen localhost:143;
protocol imap;
proxy on;
}
}
虛擬服務(wù)器配置
虛擬服務(wù)器配置文件目錄: /etc/nginx/sites-available/
自定義配置文件:sudo vim /etc/nginx/sites-available/arlingbc
配置 HTTP 服務(wù)(80端口)
# Virtual Host configuration for arlingbc.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
# 丟棄缺乏 Host 頭的請求
server {
listen 80;
return 444;
}
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# 定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置
root /var/www/example/;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
# access log file 訪問日志
access_log logs/nginx.access.log main;
# 禁止訪問隱藏文件
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# 默認(rèn)請求
location / {
# 首先嘗試將請求作為文件提供汹胃,然后作為目錄,然后回退到顯示 404乘陪。
# try_files 指令將會按照給定它的參數(shù)列出順序進(jìn)行嘗試统台,第一個被匹配的將會被使用。
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?path_info=$uri&$args =404;
access_log off;
expires max;
}
# 靜態(tài)文件啡邑,nginx 自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#過期 30 天贱勃,靜態(tài)文件不怎么更新,過期可以設(shè)大一點(diǎn),
#如果頻繁更新贵扰,則可以設(shè)置得小一點(diǎn)仇穗。
expires 30d;
}
# .php 請求
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
# PHP 腳本請求全部轉(zhuǎn)發(fā)到 FastCGI 處理. 使用 FastCGI 默認(rèn)配置.
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# 拒絕訪問. htaccess 文件,如果 Apache 的文檔根與 nginx 的一致
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
配置 HTTPS 服務(wù)(443端口)
##
# 80 port
##
# 默認(rèn)服務(wù)器戚绕,丟棄缺乏 Host 頭的請求
server {
listen 80;
return 444;
}
server {
listen 80;
listen [::]:80;
sever_name example.com www.example.com;
rewrite ^(.*)$ https://$host$1 permanent; ## 端口轉(zhuǎn)發(fā)纹坐,301 重定向
}
##
# 443 port
##
server {
##
# 阿里云參考配置
##
listen 443;
listen [::]:443;
server_name example.com www.example.com;
root /var/www/example/; # 為虛擬服務(wù)器指明文檔的根目錄
index index.html index.htm; # 給定URL文件
##
# 部署 HTTP 嚴(yán)格傳輸安全(HSTS)
##
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload;"
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
gzip off;
##
# SSL configuration
##
ssl on;
ssl_certificate cert/certfile.pem; # 證書
ssl_certificate_key cert/certfile.key; # 私鑰
ssl_session_timeout 5m; # 設(shè)置超時時間
# 密碼套件配置
# 密碼套件名稱構(gòu)成:密鑰交換-身份驗(yàn)證-加密算法(算法-強(qiáng)度-模式)-MAC或PRF
ssl_ciphers ECDHE-RSA-AES128-GCM- SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.2; # 設(shè)置 SSL/TSL 協(xié)議版本號
ssl_prefer_server_ciphers on; # 控制密碼套件優(yōu)先級,讓服務(wù)器選擇要使用的算法套件
ssl_buffer_size 1400; # 減少TLS緩沖區(qū)大小舞丛,可以顯著減少首字節(jié)時間(《HTTPS權(quán)威指南》P416)
##
# location configuration
##
# ...
}
HSTS HTTP 頭部標(biāo)準(zhǔn):
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload;
參數(shù):
- max-age耘子,服務(wù)器告訴某個客戶端,在 31536000 秒內(nèi)球切,應(yīng)該實(shí)施 HSTS 標(biāo)準(zhǔn)谷誓,這段時間內(nèi)如果客戶端重新訪問了 HTTPS 網(wǎng)站,max-age 時間就會被重新刷新吨凑。
- includeSubDomains捍歪,服務(wù)器告訴客戶端域名下的所有子域名都實(shí)施 HSTS 標(biāo)準(zhǔn),不僅僅是發(fā)出 HSTS HTTP 頭部的主機(jī)才遵循該標(biāo)準(zhǔn)鸵钝。
- preload糙臼,瀏覽器預(yù)存了需要實(shí)施 HSTS 標(biāo)準(zhǔn)的網(wǎng)站。
ngx_http_limit_conn_module 模塊
http {
# 最大連接數(shù)
# 分配一個共享內(nèi)存區(qū)域恩商,大小為 10M变逃,用于限制IP
# 使用 $binary_remote_addr 變量, 可以將每條狀態(tài)記錄的大小減少到 64 個字節(jié)痕届,這樣 1M 的內(nèi)存可以保存大約 1 萬 6 千個 64 字節(jié)的記錄韧献。
limit_conn_zone $binary_remote_addr zone=ips:10m;
# 分配一個共享內(nèi)存區(qū)域末患,大小為 10M研叫,用于限制服務(wù)器連接數(shù)
limit_conn_zone $server_name zone=servers:10m;
# 設(shè)置日志記錄級別
# 當(dāng)服務(wù)器因?yàn)轭l率過高拒絕或者延遲處理請求時可以記下相應(yīng)級別的日志。
limit_conn_log_level notice;
server {
# 限制每一個IP地址訪問限制10個連接
limit_conn ips 10;
# 服務(wù)器提供的最大連接數(shù) 1000
limit_conn servers 1000;
}
}
ngx_http_limit_req_module 模塊
http {
# 最大連接數(shù)
# 分配一個共享內(nèi)存區(qū)域璧针,大小為 10M嚷炉,限制下載連接數(shù)為1
limit_conn_zone $binary_remote_addr zone=connections:10m;
# 最大并發(fā)數(shù),每秒請求數(shù)(r/s)探橱,每分鐘請求數(shù)(r/m)
# 分配一個設(shè)置最大并發(fā)數(shù)的內(nèi)存區(qū)域申屹,大小 10M,limit_req 限制之前的請求速率 1次/s隧膏。
# 使用 $binary_remote_addr 變量哗讥, 可以將每條狀態(tài)記錄的大小減少到 64 個字節(jié),這樣 1M 的內(nèi)存可以保存大約 1 萬 6 千個 64 字節(jié)的記錄胞枕。
limit_req_zone $binary_remote_addr zone=requests:10m rate=1r/s;
# 設(shè)置日志記錄級別
# 當(dāng)服務(wù)器因?yàn)轭l率過高拒絕或者延遲處理請求時可以記下相應(yīng)級別的日志杆煞。
limit_req_log_level warn;
# immediately release socket buffer memory on timeout
reset_timedout_connection on;
server {
# 僅對 search URL 有效
location /search {
# 限制速率
# 最大延遲請求數(shù)量 10 個,超過則返回狀態(tài)碼 503
limit_req zone=requests burst=3 nodelay;
}
# 限制客戶端帶寬,
# 策略:允許小文件自由下載决乎,但對于大文件則啟用這種限制
location /downloads {
# 首先限制客戶端的下載連接數(shù)為1
limit_conn connections 1;
# 下載完 1M 內(nèi)容之后队询,啟用 limit_rate 限制。
limit_rate_after 1m;
# 限制客戶端下載下載內(nèi)容的速率為 500k/s
limit_rate 500k;
}
}
}
參考
- 阿里出品:Nginx 開發(fā)從入門到精通
- Nginx 入門指南
- Nginx 中文文檔
- NGINX 3rd Party Modules
- Nginx 反向代理(端口轉(zhuǎn)發(fā))
- 5 個提高 Node.js 應(yīng)用性能的技巧
- 基于 Debian 系統(tǒng)配置 Nginx 環(huán)境的 Node.js 應(yīng)用教程
- 為什么要使用 Node.js
- 巨頭終極對決构诚,Apache蚌斩、Nginx 與 Node.js 之爭
- 圖解 HTTPS
- nginx 強(qiáng)制使用 https 訪問 (http 跳轉(zhuǎn)到 https)
- Nginx 啟用 OCSP Stapling 的配置
- 在 NginX 上為證書配置 OCSP Stapling
- Linux 公社:Nginx 配置項(xiàng)優(yōu)化詳解