Nginx配置杂记

Nginx 配置杂记

我的nginx.conf

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

user root owner;
#user nobody;
worker_processes 1;

# pid /logs/nginx/nginx.pid;

# events-模块(工作模式&连接上限): https://www.zybuluo.com/phper/note/89391#events-%E6%A8%A1%E5%9D%97
events {
use kqueue; #mac平台
worker_connections 1024;
}


# # 这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。
# open_file_cache max=65535 inactive=60s;
# # 检查缓存有效信息的频率。
# open_file_cache_valid 80s;


http {
include mime.types;
default_type application/octet-stream;

underscores_in_headers on; # 开启变量下划线支持

client_max_body_size 24M;
client_body_buffer_size 256k;

# 日志格式设置: https://juejin.im/post/59f94f626fb9a045023af34c
# $remote_addr 客户端地址 211.28.65.253
# $remote_user 客户端用户名称 --
# $time_local 访问时间和时区 18/Jul/2012:17:00:01 +0800
# $request 请求的URI和HTTP协议 "GET /article-10000.html HTTP/1.1"
# $http_host 请求地址,即浏览器中你输入的地址(IP或域名) www.wang.com 192.168.100.100
# $status HTTP请求状态 200
# $upstream_status upstream状态 200
# $body_bytes_sent 发送给客户端文件内容大小 1547
# $http_referer url跳转来源 https://www.baidu.com/
# $http_user_agent 用户终端浏览器等信息 "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
# $ssl_protocol SSL协议版本 TLSv1
# $ssl_cipher 交换数据中的算法 RC4-SHA
# $upstream_addr 后台upstream的地址,即真正提供服务的主机地址 10.10.10.100:80
# $request_time 整个请求的总时间 0.205
# $upstream_response_time 请求过程中,upstream响应时间 0.002
log_format main 'remote_addr: $remote_addr\n'
'remote_user: $remote_user\n'
'time_local: $time_local\n'
'request: $request\n'
'status: $status\n'
'body_bytes_sent: $body_bytes_sent\n'
'http_referer: $http_referer\n'
'http_user_agent: $http_user_agent\n'
'http_x_forwarded_for: $http_x_forwarded_for\n'
'request_body: $request_body\n'
'------------------------------------------------------------------------';

log_format porxy 'http_x_forwarded_for: $http_x_forwarded_for\n'
'remote_user: $remote_user\n'
'time_local: $time_local\n'
'http_host: $http_host\n'
'request: $request\n'
'status: $status\n'
'body_bytes_sent: $body_bytes_sent\n'
'http_referer: $http_referer\n'
'http_user_agent: $http_user_agent\n'
'proxy_protocol_addr: $proxy_protocol_addr\n'
'request_body: $request_body\n'
'porxy_url: $porxy_url\n'
'------------------------------------------------------------------------';

log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';

access_log /logs/nginx/access.log main;
# error_log /logs/nginx/error.log;
# error_log /logs/nginx/error.log notice;
error_log /logs/nginx/error.log info;

access_log on;
sendfile on;
# tcp_nopush on;
# fastcgi_intercept_errors on;

# keepalive_timeout 0;
keepalive_timeout 65;

# # gzip压缩功能设置
# gzip on;

proxy_redirect off; # 关闭后端返回的header修改
proxy_set_header Host $host; # 修改发送到后端的header的host
proxy_set_header X-Real-IP $remote_addr; # 设置真实ip
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60; # 代理服务器和后端真实服务器握手连接超时时间
proxy_read_timeout 600; # 后端服务器回传数据给Nginx的时间,需要在设置的时间范围内发送完所有数据,否则Nginx将断开连接
proxy_send_timeout 600; # 代理服务器和后端服务器连接成功后,等待后端服务器响应时间

# # 负载均衡: https://www.zybuluo.com/phper/note/90310#3%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1
# # 设定负载均衡后台服务器列表
# upstream backend {
# #ip_hash; # 记录并访问上一次访问过的服务器
# server 192.168.0.100:8080 weight=10 max_fails=2 fail_timeout=30s;
# server 192.168.0.101:8080 weight=10 max_fails=2 fail_timeout=30s;
# }

server {
listen 1024;
server_name localhost;

#charset koi8-r;
charset utf-8;

access_log /logs/nginx/host.access.log main;

# location匹配: https://segmentfault.com/a/1190000013267839
location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 404 403 /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;

# ### 下面都是次要关注项
# proxy_set_header Host $host; # 修改发送到后端的header的host
# proxy_set_header X-Real-IP $remote_addr; # 设置真实ip
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 经过的IP列表
# 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;
# # 显形/隐形 URI,上游发生重定向时,Nginx 是否同步更改 uri
# proxy_redirect on | off;
# }

# 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;
# }
#}


include servers/*;
}

servers/zhaopin.conf

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
# Location 配置demo
# https://segmentfault.com/a/1190000009651161
# 负载均衡: https://www.zybuluo.com/phper/note/90310#3%E8%B4%9F%E8%BD%BD%E5%9D%87%E8%A1%A1

# upstream backend {
# #ip_hash; # 记录并访问上一次访问过的服务器
# server 192.168.0.100:8080 weight=10 max_fails=2 fail_timeout=30s;
# server 192.168.0.101:8080 weight=10 max_fails=2 fail_timeout=30s;
# }

server {
listen 80;

server_name *.zhaopin.com;
resolver 127.0.0.1 ipv6=off;

access_log /logs/nginx/$http_host.access.log porxy;
error_log /logs/nginx/zhaopin.error.log;

location ~ /bolemanage/(.*) {
proxy_pass http://192.168.214.110:8089/bolemanage/$1?$args;
# proxy_pass http://bole-api-dev.zhaopin.com/bolemanage/$1?$args;

proxy_set_header Host bole-api-dev.zhaopin.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location / {
resolver 127.0.0.1 ipv6=off;

if ($http_host ~* ^(.*-local)(\.zhaopin\.com)$) {
set $porxy_url $1$2:8000;
}
# proxy_pass http://$porxy_url;
proxy_pass http://127.0.0.1:8000;

access_log /logs/nginx/$porxy_url.access.log porxy;
error_log /logs/nginx/zhaopin-porxy_url.error.log;

proxy_set_header Host $http_host:8000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

error_page 404 403 https://github.com/404;
}

# server {
# listen 80;
# server_name rd5-dev.zhaopin.com;

# location /api/rd/rd55/head {
# proxy_pass http://rd5-pre.zhaopin.com;
# }
# }

# server {
# listen 80;

# # 泛域名解析
# server_name *.zhaopin.com;
# resolver 127.0.0.1 ipv6=off;

# # 最末级域名作为localhost端口号
# if ($host ~* ^([^\.]+)\.([^\.]+\.[^\.]+)$) {
# set $subdomain $1;
# set $domain $2;
# set $porxy_url 127.0.0.1:$1;
# }

# access_log /logs/nginx/$subdomain.zhaopin.access.log porxy;
# error_log /logs/nginx/zhaopin.error.log;

# location / {
# proxy_pass http://$porxy_url;
# }

# error_page 404 403 https://github.com/404;
# }
坚持原创技术分享,您的支持将鼓励我继续创作!
  • 本文作者: Leo
  • 本文链接: https://xuebin.me/posts/b4465dee.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!