nginx简介
nginx的工作模型: 1*maser process + N*worker process
主进程的作用主要在于读取配置文件,然后应用,并管理工作进程。
nginx是事件驱动的。
通常,nginx的配置文件nginx.conf位于 /usr/local/nginx/conf, /etc/nginx, /usr/local/etc/nginx.
当执行nginx -s reload
, 主进程解析配置文件,检查语法,然后启动新的工作进程,停止旧的工作进程。
配置文件
简单结构
通常情况下,HTTP服务器需要返回的文件对应于计算机上的多个目录。
比如,/data/www 包含HTML文件, /data/images包含图片文件,可以按照下面的结构进行配置:
http {
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
其中,server根据listen的端口进行区分,一个http模块中可以包含多个server配置。默认情况下,server listen 80端口。
在server中,可以配置多个location,nginx总是按照最长路径进行匹配。
在上面的例子中,用户访问 localhost/images/example.png, 对应的路径是/data/images/example.png, 即先查找匹配,然后将url附在root上。
localhost/some/example 则返回 /data/www/some/example
静态文件配置
root指令:配置uri查找的根目录
基本配置语法:
location URI {
root PATH_TO_ROOT
}
如果URI以/结尾,则认为这是个目录,因此可以返回一批文件。
作为代理服务器配置
nginx作为代理服务器时,会将请求转发到目标服务器上,然后将返回发送给用户。
server {
listen 8080;
root /data/up1;
location / {
proxy_pass http://127.0.0.1:8001;
}
location /images/ {
root /data;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}
上面的root指令是location的默认值, 它会影响 所有不设置root的location。
~表示一个正则表达式的匹配。
Name-based
nginx可根据请求的Host头部来路由请求
server {
listen 80 default_server;
server_name example.org www.example.org;
...
}
server {
listen 80;
server_name example.net www.example.net;
...
}
server {
listen 80;
server_name example.com www.example.com;
...
}
作为负载均衡使用
http {
upstream myapp1 {
server srv1.example.com;
server srv2.example.com;
server srv3.example.com;
}
server {
listen 80;
location / {
proxy_pass http://myapp1;
}
}
}
配置upstream映射到多个服务器,然后在location中引用,默认的负载均衡算法是 round-robin
配置HTTPS服务器
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate www.example.com.crt;
ssl_certificate_key www.example.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
...
}
例子
静态文件
http {
server {
location / {
root /data/www;
}
location /images/ {
root /data;
}
}
}
配置通用用户认证
在Java中我们可以通过配置Filter
来拦截请求,验证用户的身份。nginx
同样可以通过配置来完成对请求的用户身份进行验证,从而使得后端服务无需再做额外的校验。
提供认证功能的是nginx的ngx_http_auth_request_module
模块。其基本原理是,对每一个请求,都会向一个配置好的认证服务器发送认证请求,如果认证服务返回2xx
,则校验通过;如果为401
或403
,则表示认证失败;其他错误码一律视为请求错误。
参考文档:Nginx Module ngx_http_auth_request_module
默认情况下,nginx没有包含这个模块,需要在编译时加上:
./configure --with-http_auth_request_module
make
例子:
location /private/ {
auth_request /auth;
...
}
location = /auth {
proxy_pass ...
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
指令:
auth_request uri;
指定认证的uri,可用在http, server,location中。
auth_reqeust_set $variable value;
设置透传变量
注意,auth_request_set
必须与auth_request
出现在同级,这样才能保证所有的auth_request_set
的变量都是可用的。
从nginx返回
location / {
return 200 'gangnam style!';
# because default content-type is application/octet-stream,
# browser will offer to "save the file"...
# if you want to see reply in browser, uncomment next line
# add_header Content-Type text/plain;
}
重定向
add_header X-Frame-Options SAMEORIGIN;
return 302 http://www.domain.com/newpage;
nginx变量
http://nginx.org/en/docs/varindex.html
$request_uri
请求链接,包含完整的参数
$request_method
请求方法, GET
,POST
等
$scheme
请求协议,http
或https
$upstream_http_xxx
从上游返回的响应