nginx配置详解

这是我参与11月更文挑战的第21天,活动详情查看:2021最后一次更文挑战

nginx是一种服务端的负载均衡代理,Http代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理。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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
ini复制代码#user  nobody;   #配置用户或者组,默认为nobody nobody。
worker_processes 1; #允许生成的进程数,默认为1

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #制定日志路径,级别

#pid logs/nginx.pid; #指定nginx进程运行文件存放地址

events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
worker_connections 65535; #最大连接数,默认为512
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
}

http {
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认文件类型,默认为text/plain

#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #自定义格式

#access_log logs/acc.log main; #combined为日志格式的默认值

sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块
tcp_nopush on;
tcp_nodelay on;

#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块

upstream fzjh {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备 若无则是负载均衡
}
server {
listen 8000; #监听端口
server_name localhost; #监听地址

charset utf-8;

#access_log logs/host.access.log main; #combined为日志格式的默认值

location / { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://127.0.0.1:3000; #指向地址
# proxy_pass fzjh ; 请求转向mysvr 负载均衡
# deny 127.0.0.1; #拒绝的ip
# allow 172.18.5.54; #允许的ip
}

location /check_token { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
proxy_pass http://127.0.0.1:8088; #指向地址
# proxy_redirect on;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location /logout { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。

proxy_pass http://127.0.0.1:8088; #指向地址
# proxy_redirect on;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
}

location ~ \.aaaa$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。

rewrite ^/([\w]+)/(.*)\.air$ /$2 break ;
proxy_pass http://127.0.0.1:8088;
# proxy_redirect on;
proxy_set_header Host $host ;
proxy_set_header X-Real-IP $remote_addr ;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html { #错误页
root html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

2.其他配置

  1. include mime.types; #文件扩展名与文件类型映射表
  2. default_type application/octet-stream; #默认文件类型,默认为text/plain
  3. #access_log off; #取消服务日志
  4. log_format myFormat ‘ remoteaddr–remote_addr–remotea​ddr–remote_user [timelocal]time_local] timel​ocal]request statusstatus statusbody_bytes_sent httprefererhttp_referer httpr​efererhttp_user_agent $http_x_forwarded_for’; #自定义格式
  5. access_log log/access.log myFormat; #combined为日志格式的默认值
  6. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  7. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  8. keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
  9. proxy_connect_timeout 1; #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒
  10. proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。
  11. proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。
  12. proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。
  13. #proxy_method get; #支持客户端的请求方法。post/get;
  14. proxy_ignore_client_abort on; #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。
  15. proxy_ignore_headers “Expires” “Set-Cookie”; #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。
  16. proxy_intercept_errors on; #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。
  17. proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。
  18. proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。
  19. proxy_next_upstream timeout; #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
  20. #proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%