简介
Nginx 的安装就不解释了,方便起见,建议在各平台可以直接执行对应安装命令:
yum install nginx;
sudo apt-get install nginx;
brew install nginx;
|
一般可以在 /etc/nginx/nginx.conf
中配置,启动参数为:
nginx -s start;
nginx -s reload;
nginx -s stop;
nginx -t;
|
-s
,signal,意思就是向 nginx 发送 start|reload|stop
命令,还是很好理解的。先看一个最简单的 nginx.conf
配置:
events { } http { server { listen 127.0.0.1:8888; location / { root /home/barret/test/; } } }
|
启动后,访问 htttp://127.0.0.1:8888 ,如果 /home/barret/test/
下有 index.html
文件就会展示 index.html
的内容,否则返回 404
。
Nginx 配置一个 Web 服务器
以下对配置 Web 服务器的参数做简单说明,包括如何配置端口、域名,如何处理请求,如何响应请求。
1、 虚拟主机和请求的分发
域名和端口的配置
listen 127.0.0.1:8000; listen *:8000; listen localhost:8000;
listen [::]:8000;
listen 443 default_serer ssl; listen 127.0.0.1 default_server accept_filter=dataready backlog=1024
|
主机名配置
server_name www.barretlee.com barretlee.com server_name *.barretlee.com server_name ~^\.barret\.com$
|
URI 匹配
location = / { } location ^~ /images/ { } location / { }
|
2、 文件路径的定义
根目录设置
location / { root /home/barret/test/; }
|
别名设置
location /blog { alias /home/barret/www/blog/; } location ~ ^/blog/(\d+)/([\w-]+)$ { alias /home/barret/www/blog/$1-$2.md; }
|
首页设置
index /html/index.html /php/index.php;
|
重定向页面设置
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 设置
try_files $uri $uri.html $uri/index.html @other; location @other { proxy_pass http://localhost:9000; } location / { try_files $uri $uri.html =502; }
|
Nginx 配置反向代理服务器
反向代理(reserve proxy)方式是指用代理服务器来接受 Internet 上的连接请求,然后将请求转发给内部网络中的上游服务器,并将上游服务器上得到的结果返回给 Internet 上请求连接的客户端,此时代理服务器对外的表现就是一个 Web 服务器。
Nginx 具备超强的高并发高负载能力,一般会作为前端的服务器直接向客户端提供静态文件服务;而业务一般还包含一些业务逻辑需要 Apache、Tomcat 等服务器来处理,故通常 Nginx 对外表现即为静态 Web 服务器也是反向代理服务器。
缺点是增加了一次请求的处理时间,优点是降低了上游服务器的负载,尽量将压力放在 Nginx 服务器上。
1、负载均衡配置
upstream,定义一个上游服务器集群
upstream backend { server s1.barretlee.com; server s2.barretlee.com; } server { location / { proxy_pass http://backend; } }
|
2、反向代理
proxy_pass 将请求转发到有处理能力的端上,默认不会转发请求中的 Host 头部
location /blog { prox_pass http://localhost:9000;
proxy_set_header Host $host; proxy_method POST; proxy_hide_header Cache-Control; proxy_hide_header Other-Header; proxy_pass_header Server-IP; proxy_pass_header Server-Name; proxy_pass_request_body on | off; proxy_pass_request_headers on | off; proxy_redirect on | off; }
|
一个简单的例子,Node.js
一个十分常见的需求:处理请求,如果是静态文件,Nginx 直接返回,否则交给 Node 服务器处理。首先创建了一个 Node 服务器:
const http = require('http'); http.createServer((req, res) => { res.end('hello world'); }).listen(9000);
|
任何请求过来都返回 hello world
,简版的 Nginx 配置如下,
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 的状态码。
原文转载至:https://www.barretlee.com/blog/2016/11/19/nginx-configuration-start/