域名重定向 重定向flftuu.site -> flftuu.com
1 2 3 4 5 server { listen 80; server_name flftuu.site, www.flftuu.site; rewrite "^/(.*)$" https://www.flftuu.com/$1 permanent; }
nginx 虚拟机主机 http 选项中包含多个 server server 中server_name 设置不通域名实现多虚拟机功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 server { }
nginx 配置详解 在了解具体的Nginx配置项之前我们需要对于Nginx配置文件的构成有所概念,一般来说,Nginx配置文件会由如下几个部分构成:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 ... events { ... } http { ... server { ... location [PATTERN] { ... } location [PATTERN] { ... } } server { ... } ... }
在上述配置中我们可以看出,Nginx配置文件由以下几个部分构成:
全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
server块:配置虚拟主机的相关参数,一个http中可以有多个server。
location块:配置请求的路由,以及各种页面的处理情况。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 error_log log /error.log debug; events { accept_mutex on; multi_accept on; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for' ; access_log log /access.log myFormat; sendfile on; sendfile_max_chunk 100k; keepalive_timeout 65; upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333 backup; } error_page 404 https://www.baidu.com; server { keepalive_requests 120; listen 4545; server_name 127.0.0.1; location ~*^.+$ { proxy_pass http://mysvr; deny 127.0.0.1; allow 172.18.5.54; } } }
虚拟主机与静态站点 官方文档
本部分概述如何配置Nginx进行静态内容服务,Nginx的静态内容分发能力还是非常强大的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 http { server { listen 80; server_name www.domain1.com; access_log logs/domain1.access.log main; location / { index index.html; root /var/www/domain1.com/htdocs; } } server { listen 80; server_name www.domain2.com; access_log logs/domain2.access.log main; location / { index index.html; root /var/www/domain2.com/htdocs; } } }
虚拟主机配置详解 主机与端口 1 2 3 4 5 6 server_name www.barretlee.com barretlee.com; server_name *.barretlee.com; server_name ~^\.barret\.com$;
URI匹配 1 2 3 4 5 6 7 8 9 10 11 12 13 location = / { } location ^~ /images/ { } location / { }
文件路径配置 根目录 1 2 3 location / { root /home/barret/test/; }
别名 1 2 3 4 5 6 7 8 location /blog { alias /home/barret/www/blog/; } location ~ ^/blog/(\d+)/([\w-]+)$ { alias /home/barret/www/blog/$1 -$2 .md; }
首页 1 index /html/index.html /php/index.php;
重定向页面 1 2 3 4 5 6 7 8 9 10 error_page 404 /404.html; error_page 502 503 /50x.html; error_page 404 =200 /1x1.gif; location / { error_page 404 @fallback; } location @fallback { proxy_pass http://localhost:9000; }
try_files 1 2 3 4 5 6 7 8 9 try_files $uri $uri .html $uri /index.html @other; location @other { proxy_pass http://localhost:9000; } location / { try_files $uri $uri .html =502; }
缓存配置 缓存
Expire:过期时间 在Nginx中可以配置缓存的过期时间:
1 2 3 4 5 location ~* \.(?:ico|css|js|gif|jpe?g|png)$ { expires 30d; add_header Vary Accept-Encoding; access_log off; }
我们也可以添加更复杂的配置项:
1 2 3 4 5 6 7 8 9 10 11 12 13 location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|svg)$ { access_log off; expires 30d; tcp_nodelay off; open_file_cache max=3000 inactive=120s; open_file_cache_valid 45s; open_file_cache_min_uses 2; open_file_cache_errors off; }
反向代理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 events{ } http{ upstream ggzy { server 127.0.0.1:1398 weight=3; server 127.0.0.1:1399; } server { listen 80; index index index.htm index.py index.html; server_name app.truelore.cn; location / { proxy_pass_header Server; proxy_set_header Host $http_host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Scheme $scheme ; proxy_pass http//ggzy; } } }
NodeJS Application 1 2 3 4 const http = require('http' ); http.createServer((req, res) => { res.end('hello world'); }).listen(9000 );
任何请求过来都返回 hello world,简版的 Nginx 配置如下,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 events { use epoll; } http { server { listen 127.0.0.1:8888; try_files $uri $uri /index.html; location ~* ^/(js|css|image|font)/$ { root /home/barret/www/static/; } location /app { proxy_pass http://127.0.0.1:9000; } error_page 404 /404.html; error_page 502 503 504 /50x.html; } }
首先 try_files,尝试直接匹配文件;没找到就匹配静态资源;还没找到就交给 Node 处理;否则就返回 4xx/5xx 的状态码。
Upstream Cache A Guide to Caching with NGINX and NGINX Plus
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 http { ,,,,, proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=imgcache:100m inactive=1d max_size=10g; server { ........ location ~* ^.+\.(js|ico|gif|jpg|jpeg|png|html|htm)$ { log_not_found off; access_log off; expires 7d; proxy_pass http://img.example.com ; proxy_cache imgcache; proxy_cache_valid 200 302 1d; proxy_cache_valid 404 10m; proxy_cache_valid any 1h; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } }
HTTPS HTTPS 理论详解与实践
https证书生成方式
基本HTTPS配置 基本的HTTPS支持配置如下:
1 2 3 4 5 6 7 8 9 10 server { listen 192.168.1.11:443; server_name test.com; ssl on; ssl_certificate /etc/nginx/test.pem; ssl_certificate_key /etc/nginx/test.key; }
在真实的生产环境中,我们的配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 server { listen 443 ssl; server_name www.vpser.net; index index.html index.htm index.php default.html default.htm default.php; root /home/wwwroot/www.vpser.net; ssl_certificate /etc/letsencrypt/live/www.vpser.net/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.vpser.net/privkey.pem; ssl_ciphers "EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5" ; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; include wordpress.conf; location ~ [^/]\.php(/|$) { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log off; }
强制HTTP转到HTTPS Nginx Rewrite
1 2 3 4 5 server { listen 192.168.1.111:80; server_name test.com; rewrite ^(.*)$ https://$host$1 permanent; }
Nginx 497错误码 利用error_page命令将497状态码的链接重定向到https://test.com这个域名上
1 2 3 4 5 6 7 8 9 10 11 12 13 14 server { listen 192.168.1.11:443; listen 192.168.1.11:80; server_name test.com; ssl on; ssl_certificate /etc/nginx/test.pem; ssl_certificate_key /etc/nginx/test.key; error_page 497 https://$host$uri ?$args ; }
在HTTP正常返回的页面中添加meta属性:
1 2 3 <html > <meta http-equiv ="refresh" content ="0;url=https://test.com/" > </html >
1 2 3 4 5 6 7 8 9 10 11 server { listen 192.168.1.11:80; server_name test.com; location / { root /srv/www/http.test.com/; } error_page 404 https://test.com/; }