我是靠谱客的博主 优美枕头,这篇文章主要介绍nginx实现http服务配置,现在分享给大家,希望可以做个参考。

静态文件

即实现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
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; server { listen 80; server_name localhost; location /images { root /Users/jun/data; } location / { root /Users/jun/data/www; index index.html index.htm; } } include servers/*; }

http://localhost/images/jie.png的请求会匹配上面的location,返回本地/Users/jun/data/images/jie.png的图片,
http://localhost/ 的请求会匹配到下面的location,然后返回本地/Users/jun/data/www/下面的index.html文件。

开始一直搞不懂文件匹配规则,其实就是匹配到相应的location后,把请求url拼接到root的参数后去本地文件系统中查找,如刚才http://localhost/请求,返回/Users/jun/data + //Users/jun/data/下的index.html(缺省文件),对于http://localhost/images/jie.png请求 就返回/Users/jun/data/www + /images/jie.png 即服务器本地/Users/jun/data/images/jie.png的图片。

至于匹配哪个location,当符合多个locating的匹配规则时,选取最长的那个,如上面http://localhost/images/jie.png的请求其实两个location规则都符合,但nginx会选取前缀最长的那个/images。再比如http://localhost/some/example.png请求就会去寻找/Users/jun/data/www/some/example.png

负载均衡的配置

如果应用程序以集群方式部署,我们需要采取负载均衡,来看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
http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; default_type application/octet-stream; #设定日志格式 access_log /var/log/nginx/access.log; #设定负载均衡的服务器列表 upstream load_balance_server { #weigth参数表示权值,权值越高被分配到的几率越大 server 192.168.1.11:80 weight=5; server 192.168.1.12:80 weight=1; server 192.168.1.13:80 weight=6; } #HTTP服务器 server { #侦听80端口 listen 80; #定义使用www.xx.com访问 server_name www.helloworld.com; #对所有请求进行负载均衡请求 location / { root /root; #定义服务器的默认网站根目录位置 index index.html index.htm; #定义首页索引文件的名称 proxy_pass http://load_balance_server ;#请求转向load_balance_server 定义的服务器列表 #以下是一些反向代理的配置(可选择性配置) #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Forwarded-For $remote_addr; proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时) proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时) proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小 proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置 proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传 client_max_body_size 10m; #允许客户端请求的最大单文件字节数 client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数 } } }

2.3 https反向代理配置

现在越来越多的网站采用https协议来提高应用的安全性,所以我们需要知道nginx里面https如何配置。
首先要注意:HTTPS 的固定端口号是 443,不同于 HTTP 的 80 端口SSL 标准需要引入安全证书,所以在 nginx.conf 中你需要指定证书和它对应的 key。

复制代码
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
upstream tomcat{ ip_hash; server 127.0.0.1:9080; } #控制全局nginx所有请求报文大小 client_max_body_size 2m; server { listen 80; server_name 127.0.0.1; #charset koi8-r; #access_log logs/host.access.log main; location /web/database/back/restoreDataBaseByFileName { rewrite ^ https://$server_name:443$request_uri?permanent; } location / { rewrite ^ https://$server_name:443$request_uri?permanent; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 443 ssl; server_name 127.0.0.1; ssl_certificate ..\ca\server\server.cer; ssl_certificate_key ..\ca\server\server.key; ssl_protocols TLSv1.2; ssl_ciphers 'XXXX-RSA-AES128-GCM-SHA256:-ECDSA-AES256-SHA:-RSA-AES128-SHA256:-RSA-AES128-SHA:DHE-DSS-DSS-AES256-SHA:-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SA-XXXX'; ssl_session_cache shared:SSL:1m; ssl_session_timeout 30m; charset utf-8; ssl_dhparam \dhparams.pem; #access_log logs/host.access.log main; location /web/database/back/restoreDataBaseByFileName { root html; index index.html index.htm; proxy_http_version 1.1; chunked_transfer_encoding off; proxy_connect_timeout 6000; proxy_read_timeout 6000; proxy_send_timeout 6000; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; 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://tomcat; } location / { root html; index index.html index.htm; proxy_http_version 1.1; chunked_transfer_encoding off; proxy_connect_timeout 60; proxy_read_timeout 60; proxy_send_timeout 60; proxy_buffer_size 16k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400; 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://tomcat; } #error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

2.4 多个webapp部署到同一机器不同端口

当一个网站功能越来越丰富时,往往需要将一些功能相对独立的模块剥离出来,独立维护。这样的话,通常,会有多个 webapp。

举个例子:假如 www.helloworld.com 站点有好几个 webapp,finance(金融)、product(产品)、admin(用户中心)。访问这些应用的方式通过上下文(context)来进行区分:

www.helloworld.com/finance/

www.helloworld.com/product/

www.helloworld.com/admin/

我们知道,http 的默认端口号是 80,如果在一台服务器上同时启动这 3 个 webapp 应用,都用 80 端口,肯定是不成的。所以,这三个应用需要分别绑定不同的端口号。

那么,问题来了,用户在实际访问 www.helloworld.com 站点时,访问不同 webapp,总不会还带着对应的端口号去访问吧。所以,你再次需要用到反向代理来做处理。

配置也不难,来看看怎么做吧:

复制代码
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
http { #此处省略一些基本配置 upstream product_server{ server www.helloworld.com:8081; } upstream admin_server{ server www.helloworld.com:8082; } upstream finance_server{ server www.helloworld.com:8083; } server { #此处省略一些基本配置 #默认指向product的server location / { proxy_pass http://product_server; } location /product/{ proxy_pass http://product_server; } location /admin/ { proxy_pass http://admin_server; } location /finance/ { proxy_pass http://finance_server; } } }

转自:静默虚空博客

最后

以上就是优美枕头最近收集整理的关于nginx实现http服务配置的全部内容,更多相关nginx实现http服务配置内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部