Linux-nginx配置文件详解
目录
启动命令
./nginx #启动
./nginx -s stop #关闭
./nginx -s reload #重启
- listen: 监听端口
- server_name :是通配符,输入自己的域名IP
- root / :静态文件所在的路径
- proxy_pass:此伪指令设置侦听代理服务器的端口或套接字,以及将在其上反映位置的URI。端口可以以主机名或地址和端口名的形式表示。
- proxy_redirect:如果需要修改从被代理服务器传来的应答头中的"Location"和"Refresh"字段,可以用这个指令设置
- proxy_set_header:该指令允许重新定义并添加一些请求标头行,这些请求标头行将被传输到代理服务器。 作为值,可以使用文本,变量及其组合。 如果未在此级别描述其指令,则此指令从上一级继承。
location :
语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。以xx开头
~ 开头表示区分大小写的正则匹配 以xx结尾
~* 开头表示不区分大小写的正则匹配 以xx结尾
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置:
如果有多个匹配的location块,nginx会选择最长的前缀块
location = / {
#规则A
}
location = /login {
#规则B
}
location = /login/log {
#规则C
}
ReWrite语法:
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行
代理D盘符下的文件:
location / {
root d:wwwroot; //思路:通过/将所有的请求,转发给root处理
index index.html;
}
Redirect重定向:
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
反向代理:
代理发往服务器的请求,可以负载均衡、虚拟主机等等
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host:$server_port; //思路:通过/,将所有的请求,转发给第3方处理
}
配置upstream实现负载均衡:
1. 在http节点下,加入upstream节点。
upstream linuxidc {
server 10.0.6.108:7080;
server 10.0.0.85:8980;
}
2. 将server节点下的location节点中的proxy_pass配置为:http:// + upstream名称,即“
http://linuxidc”.
location / {
root html;
index index.html index.htm;
proxy_pass http://linuxidc/;
}
3. 如今负载均衡初步完毕了。upstream依照轮询(默认)方式进行负载,每一个请求按时间顺序逐一分配到不同的后端服务器。假设后端服务器down掉。能自己主动剔除。尽管这样的方式简便、成本低廉。但缺点是:可靠性低和负载分配不均衡。
适用于图片服务器集群和纯静态页面服务器集群。
除此之外,upstream还有其他的分配策略,分别例如以下:
weight(权重)
指定轮询几率,weight和訪问比率成正比,用于后端服务器性能不均的情况。例如以下所看到的。10.0.0.88的訪问比率要比10.0.0.77的訪问比率高一倍。
upstream linuxidc{
server 10.0.0.77 weight=5;
server 10.0.0.88 weight=10;
}
fair(第三方)
按后端服务器的响应时间来分配请求。响应时间短的优先分配。
与weight分配策略相似。
upstream favresin{
server 10.0.0.10:8080;
server 10.0.0.11:8080;
fair;
}
url_hash(第三方)
按訪问url的hash结果来分配请求,使每一个url定向到同一个后端服务器。后端服务器为缓存时比較有效。
注意:在upstream中加入hash语句。server语句中不能写入weight等其他的參数,hash_method是使用的hash算法。
upstream resinserver{
server 10.0.0.10:7777;
server 10.0.0.11:8888;
hash $request_uri;
hash_method crc32;
}
upstream还能够为每一个设备设置状态值,这些状态值的含义分别例如以下:
down 表示单前的server临时不參与负载.
weight 默觉得1.weight越大,负载的权重就越大。
max_fails :同意请求失败的次数默觉得1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误.
fail_timeout : max_fails次失败后。暂停的时间。
backup: 其他全部的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
upstream bakend{ #定义负载均衡设备的Ip及设备状态
ip_hash;
server 10.0.0.11:9090 down;
server 10.0.0.11:8080 weight=2;
server 10.0.0.11:6060;
server 10.0.0.11:7070 backup;
}
server1 挂掉时(返回非 200 值)去尝试获取 server2
upstream up_nodes {
server xx.xx.xx.xx:80 max_fails=0;
server xx.xx.xx.xx:80 max_fails=0;
keepalive 64;
}
server {
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
location / {
proxy_pass http://up_nodes;
break;
}
}
RR(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
在单位周期为fail_timeout设置的时间中失败次数达到max_fails次,这个周期次数内,如果后端同一个节点不可用,那么就将把节点标记为不可用,并等待下一个周期(同样时长为fail_timeout)再一次去请求,判断是否连接是否成功
upstream tomcats {
server 172.19.1.182:8888 max_fails=3 fail_timeout=3s weight=9;
server 172.19.1.152:8080 max_fails=3 fail_timeout=3s weight=9;
}
4. ip_hash(訪问ip)
每一个请求按訪问ip的hash结果分配。这样每一个訪客固定訪问一个后端服务器,能够解决session的问题。
upstream favresin{
ip_hash;
server 10.0.0.10:8080;
server 10.0.0.11:8080;
}
防盗链:
location ~* \.(gif|jpg|png|bmp)$ {
valid_referers none blocked *.ttlsa.com server_names ~\.google\. ~\.baidu\.;
if ($invalid_referer) {
return 403;
#rewrite ^/ http://www.ttlsa.com/403.jpg;
}
}
禁止访问某个目录:
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test; #所有用户都禁止访问这个目录
deny all;
}
隐藏版本号
server_tokens off;
配置示例nginx.conf:
server {
listen 80 default_server;
# Make site accessible from http://localhost/
server_name dev.baidu.com; # yu ming
root /var/www;
underscores_in_headers on;
index index.html index.htm;
include /etc/nginx/default.d/*.conf;
location /project_name/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8089/;
client_max_body_size 100m;
}
location / {
root /var/www;
index index.html;
}
error_page 404 /404.html;
}
[root@bogon conf]# cat /usr/local/webserver/nginx/conf/nginx.conf
user www www;
worker_processes 2; #设置值和CPU核心数一致
error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
pid /usr/local/webserver/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虚拟主机的配置
server
{
listen 80;#监听端口
server_name localhost;#域名
index index.html index.htm index.php;
root /usr/local/webserver/nginx/html;#站点目录
location ~ .*\.(php|php5)?$
{
#fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
{
expires 30d;
# access_log off;
}
location ~ .*\.(js|css)?$
{
expires 15d;
# access_log off;
}
access_log off;
}
}
nginx 413
在接口location块中添加其中的配置为:
client_max_body_size 200m;
location /project_name/ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8089/;
client_max_body_size 100m;
}
nginx 414配置
http {
# 声明日志格式,request_length用来输出每一个请求的大小(请求行+请求头+请求体)
log_format main '$remote_addr - $remote_user [$time_local] "$request" $request_length '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 指定访问日志的格式和存放路径
access_log /usr/local/var/log/nginx/access.log main;
# 请求行+请求头的标准大小为512k
client_header_buffer_size 512k;
# 请求行+请求头的最大大小为512k
large_client_header_buffers 4 512k;
}
-
lient_header_buffer_size
Syntax: client_header_buffer_size size;
Default: client_header_buffer_size 1k;
Context: http, server
Syntax: large_client_header_buffers number size;
Default: large_client_header_buffers 4 8k;
Context: http, server
先根据client_header_buffer_size配置的值分配一个buffer,如果分配的buffer无法容纳 request_line/request_header,那么就会再次根据large_client_header_buffers配置的参数分配large_buffer,如果large_buffer还是无法容纳,那么就会返回414(处理request_line)/400(处理request_header)错误。
假设large_client_header_buffers的配置为4 8k,则对请求有如下要求
- 请求行(request line)的大小不能超过8k,否则返回414错误
- 请求头(request header)中的每一个头部字段的大小不能超过8k,否则返回400错误(实际是494错误,但nginx统一返回400了)
curl -H "header1=aaa" -H "header2=bbb" -v http://127.0.0.1/
,这里的header1=xxx和header2=xxx就是请求头中的头部字段 - (请求行+请求头)的大小不能超过32k(4 * 8k)
Nginx配置Cookie安全策略
在Nginx中,可以通过以下方式配置Cookie安全策略
- 设置httponly属性
Cookie的httponly属性是为了防止攻击者通过JavaScript窃取Cookie而产生的。当设置httponly属性后,Cookie将无法通过JavaScript访问,只能通过HTTP请求发送至服务器。在Nginx中,可以通过将httponly属性值设置为“true”或“on”来开启该功能。
- 设置secure属性
Cookie的secure属性是为了防止在非安全的HTTP连接上(即不使用SSL/TLS加密)发送Cookie,从而导致Cookie被中间人攻击者窃取。当设置secure属性后,Cookie将只能在通过SSL/TLS加密连接的HTTPS协议上进行传输。在Nginx中,可以通过将secure属性值设置为“true”或“on”来开启该功能。
- 设置samesite属性
Cookie的samesite属性是为了防止跨站请求伪造(CSRF)攻击,通常有三个值:strict、lax和none。strict表示浏览器仅在当前网站的域名和协议完全一致的情况下才会发送Cookie;lax表示浏览器可以在某些场景下(如用户在网站内点击带有外部链接的按钮时)发送Cookie;none表示浏览器可以在任何情况下都发送Cookie。在Nginx中,可以通过将samesite属性值设置为“strict”、“lax”或“none”来开启该功能。
server {
listen 80;
server_name example.com;
location / {
# 设置Cookie的Secure属性
add_header Set-Cookie "secure=true;";
# 设置Cookie的HttpOnly属性
add_header Set-Cookie "httponly=true;";
# 设置Cookie的Path属性
add_header Set-Cookie "path=/auth;";
}
}
未完