server模块
由于这次只是学习server模块的配置,所以upstream模块的信息没有提供在文档中。以下配置中proxy_pass http:// 后的字段,为配置文件中配置的upstream名称。
server {
listen 80;
listen 443 ssl;
ssl_certificate /app/tengine/conf/key/test.pem;
ssl_certificate_key /app/tengine/conf/key/test.key;
add_header Cache-Control no-cache;
add_header Cache-Control private;
add_header X-Frame-Options SAMEORIGIN;
server_name localhost;
location /status {
check_status;
access_log off;
}
location ^~ /stock {
proxy_pass http://stock;#对应upstream未提供在文档中
proxy_method GET;
error_page 405 =200 http://127.0.0.1/$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Ssl on;
proxy_redirect off;
if ($request_filename ~* .*.(html|htm)$)
{
expires -1s;
}
if ($request_filename ~* .*.(gif|jpg|jpeg|png|bmp|swf)$)
{
expires 30d;
}
if ($request_filename ~ .*.(js|css)$)
{
expires 12h;
}
}
location / {
alias /app/appData/;
if ($request_filename ~* .*.(html|htm)$)
{
expires -1s;
}
if ($request_filename ~* .*.(gif|jpg|jpeg|png|bmp|swf)$)
{
expires 30d;
}
if ($request_filename ~ .*.(js|css)$)
{
expires 12h;
}
}
location /api/v1 {
proxy_pass http://web2;
proxy_set_header Accept-Encoding "";
req_status server;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
error_page 405 =200 @405;
location @405
{
root /home;
proxy_method GET;
proxy_pass http://127.0.0.1/$request_uri;
}
}
这里的server模块是指ngx_http_core_module.中的server块,用来定义一个虚拟主机。
以上的配置文件是截取了配置较有特点的location配置,下面详细学习一下。
首先看 server基础配置。
1、基础配置
listen 80; #http协议监听80端口。
listen 443 ssl; #打开ssl协议,并监听443端口。
ssl_certificate /app/tengine/conf/key/test.pem; #指定服务器端证书位置
ssl_certificate_key /app/tengine/conf/key/test.key; #指定服务器端密钥的位置
#设置浏览器不使用缓存。
add_header Cache-Control no-cache;
add_header Cache-Control private;
add_header X-Frame-Options SAMEORIGIN;#表示页面可以在相同域名的页面的frame中展示。
server_name localhost; #设置虚拟主机组名称为localhost。
- add_header 属于ngx_http_headers_module模块。ngx_http_headers_module模块允许发出“Exend”和“Cache-Control”头字段,并将任意字段添加到响应标头中,条件是响应代码等于200、201、204、206、301、302、303、304或307。值可以包含变量。
- Cache -Control指定请求和响应遵循的缓存机制。请求时的缓存指令包括no-cache、no-store、max-age、 max-stale、min-fresh、only-if-cached等。响应消息中的指令包括public、private、no-cache、no- store、no-transform、must-revalidate、proxy-revalidate、max-age。
- X-Frame-Options HTTP 响应头是用来给浏览器指示允许一个页面可否在 , 或者 中展现的标记。网站可以使用此功能,来确保自己网站的内容没有被嵌到别人的网站中去,也从而避免了点击劫持 (clickjacking) 的攻击。
2、location /status
location /ustatus {
check_status; #显示服务器的健康状态页面
access_log off; #关闭access.log的打印
}
这种配置在访问ip:port/ustatus时会显示服务器的监控状态,但是这个配置显得有点多余,开启了req_status_zone后直接在url后加/req-status,与此处配置有相同的地方。
3、location ^~ /stock
location ^~ /stock { #匹配所有以/stock开头的路径,都执行以下的操作
proxy_pass http://stock;#对应upstream未提供在文档中
proxy_method GET; #支持客户端的请求方法为GET
error_page 405 =200 http://127.0.0.1/$request_uri; #为指令错误定义显示的URI 将405的返回改成200 并返回http://127.0.0.1/$request_uri
proxy_set_header Host $host; #在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名
proxy_set_header X-Real-IP $remote_addr; # 设置X-Real-IP字段为客户端ip,$remote_addr只是代理的上一层地址。
proxy_set_header X-Forwarded-Proto $scheme; #设置转发的协议 为请求使用的web协议 http或https
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;#设置 X-Forwarded-For追加代理服务器的IP
proxy_set_header X-Forwarded-Ssl on; #开启https转发协议
proxy_redirect off; #禁止响应头替换文本
#以下设置缓存时间
if ($request_filename ~* .*.(html|htm)$) #不区分大小写匹配文件名以html|htm结尾的规则
{
expires -1s; #过期时间为-1说明不缓存
}
if ($request_filename ~* .*.(gif|jpg|jpeg|png|bmp|swf)$)
{
expires 30d;
}
if ($request_filename ~ .*.(js|css)$)#区分大小写 以jscss结尾
{
expires 12h;
}
}
location / {
alias /app/appData/; #使用alias路径替换location路径,即访问路径为/时,相当于访问了/app/appData
if ($request_filename ~* .*.(html|htm)$)
{
expires -1s;
}
if ($request_filename ~* .*.(gif|jpg|jpeg|png|bmp|swf)$)
{
expires 30d;
}
if ($request_filename ~ .*.(js|css)$)
{
expires 12h;
}
}
location /api/v1 {
proxy_pass http://web2;
proxy_set_header Accept-Encoding "";
req_status server;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
error_page 405 =200 @405; #405错误处理
location @405
{
root /home;
proxy_method GET;
proxy_pass http://127.0.0.1/$request_uri;
}
- proxy_set_header 允许重新定义或者添加发往后端服务器的请求头。
- request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成
- alias 处理结果 使用alias路径替换location路径,alias后面必须要用“/”结束
- error_page 当页面发生异常的时候可以指定跳转到location中,也可以指定跳转到指定的URL地址上面
- error_page 405 =200 @405; 405报错是不能使用post静态文件,所以我们请求方法转变成GET,方法将405当成200处理,转至location @405,将方法转换成GET再次请求原uri。