nginx 反向代理memcached、websocket及nginx文件方面的优化

安装memcached服务,并启动添加数据

yum -y install memcached
systemctl start memcached.service 启动

[root@python ~]# telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
set hello 0 0 5 
world
STORED
get hello
VALUE hello 0 5
world
END
set gzipkey 2 0 3
chx
STORED
get gzipkey
VALUE gzipkey 2 3
chx
END

  nginx默认就支持此功能

配置

server {
	server_name memcached.com;
	default_type text/plain;
	location /get {
		set    $memcached_key "$arg_key";  #?表示?是key
		memcached_gzip_flag 2;  # 识别压缩的内容
		memcached_pass 127.0.0.1:11211;  地址
	}
}

  测试

[root@python vhast]# curl memcached.com/get?key=gzipkey -I  #key代giz字眼
HTTP/1.1 200 OK
Server: nginx/1.15.9
Date: Thu, 18 Jul 2019 09:45:51 GMT
Content-Type: text/plain
Content-Length: 3
Connection: keep-alive
Content-Encoding: gzip  识别
Accept-Ranges: bytes

[root@python vhast]# curl memcached.com/get?key=hello -I  #没有
HTTP/1.1 200 OK
Server: nginx/1.15.9
Date: Thu, 18 Jul 2019 09:46:10 GMT
Content-Type: text/plain
Content-Length: 5
Connection: keep-alive
Accept-Ranges: bytes

  配置

[root@python vhast]# cat memcached.conf 
server {
	server_name memcached.com;
	default_type text/plain;
	location /get {
		set    $memcached_key "$arg_key";
		#memcached_gzip_flag 2;
		memcached_pass 127.0.0.1:11211;
	}
}

  测试

[root@python vhast]# curl memcached.com/get?key=gzipkey -I
HTTP/1.1 200 OK
Server: nginx/1.15.9
Date: Thu, 18 Jul 2019 09:48:12 GMT
Content-Type: text/plain
Content-Length: 3
Connection: keep-alive
Accept-Ranges: bytes

  构建websocket反向代理

配置指令

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";  

 

    协议升级

借助互联网上的http://echo.websocket.org/echo.html 

server {
        server_name cx.websocket.com;
        default_type text/plain;
        access_log logs/ws.log;
        location / {
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
                proxy_pass http://echo.websocket.org;
        }
}

  

分片提升缓存效率;slice模块默认未编译进去

指令编译进nginx里

cd ~/nginx-1.15.9/
./configure --prefix=/data/web --sbin-path=/usr/bin --user=nginx --group=nginx --with-http_stub_status_module --with-http_auth_request_module --with-http_sub_module --add-module=/root/nginx-http-concat --with-http_addition_module --with-http_secure_link_module --with-http_geoip_module  --with-http_ssl_module  --add-module=/root/ngx_cache_purge  --with-http_slice_module
[root@python nginx-1.15.9]# mv /usr/bin/nginx{,.07.18.18.33}
[root@python nginx-1.15.9]# cp objs/nginx /usr/bin/

     工作流程

 

指令介绍

Syntax: slice size;
Default: slice 0; 
Context: http, server, location
功能通过range协议将大文件分解多个小文件,更好的用缓存为客户端的range协议服务

  配置

[root@python vhast]# cat cache.conf 
proxy_cache_path /data/web/cache levels=2:2 keys_zone=two:10m loader_threshold=300 loader_files=200 max_size=200m inactive=1m;
server {
	server_name cache.com;
	error_log logs/cacgeee.log debug;
	access_log logs/cache.log main;
	root html/;
	location ~/purge(/.*) {
		proxy_cache_purge two $scheme$1;
	}
	location /{
		
		
		proxy_cache two;
		slice      1m; #切分没片大小
		proxy_cache_valid 200 206 1m;
		add_header X-Cache-Status $upstream_cache_status;
		proxy_set_header Range $slice_range;  #吧客户端协议发送到上游服务器
	
		proxy_pass http://127.0.0.1:8012;
		}
}

  open_file_cache提升系统性能

指令

Syntax: open_file_cache off;
             open_file_cache max=N [inactive=time]; 最多缓存多少文件,在内存里而非共享内存 跟时间在这个时间内没有访问就移除缓存列表
Default: open_file_cache off; 
Context: http, server, location

  缓存的内容

其他open_file_cache的指令

Syntax: open_file_cache_errors on | off;  错误的是否缓存
Default: open_file_cache_errors off; 
Context: http, server, location



Syntax: open_file_cache_min_uses number;  至少访问多少次才留在缓存中
Default: open_file_cache_min_uses 1; 
Context: http, server, location



Syntax: open_file_cache_valid time;  多长时间建成一次缓存内容是否有效
Default: open_file_cache_valid 60s; 
Context: http, server, location

  上游服务器配置

[root@python ~]# cat /data/web/conf/vhast/open.conf 
server {
	listen 8092;
	root html;
	location /{
		#open_file_cache max=10 inactive=60s;
		#open_file_cache_min_uses 1;
		#open_file_cache_valid 60s;
		#open_file_cache_errors on;
	}
}

  

使用strace追踪

[root@python vhast]# ps -ef | grep nginx
root      10241      1  0 19:16 ?        00:00:00 nginx: master process nginx
nginx     10242  10241  0 19:16 ?        00:00:00 nginx: worker process
nginx     10243  10241  0 19:16 ?        00:00:00 nginx: cache manager process
nginx     10244  10241  0 19:16 ?        00:00:00 nginx: cache loader process
root      10246   7257  0 19:16 pts/2    00:00:00 grep --color=auto nginx
[root@python vhast]# strace -p 10242
strace: Process 10242 attached
epoll_wait(25
[root@python ~]# curl 127.0.0.1:8092
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@python vhast]# strace -p 10242
strace: Process 10242 attached
epoll_wait(25, [{EPOLLIN, {u32=9643568, u64=9643568}}], 512, -1) = 1
accept4(20, {sa_family=AF_INET, sin_port=htons(36974), sin_addr=inet_addr("127.0.0.1")}, [16], SOCK_NONBLOCK) = 28
epoll_ctl(25, EPOLL_CTL_ADD, 28, {EPOLLIN|EPOLLRDHUP|EPOLLET, {u32=9644768, u64=9644768}}) = 0
epoll_wait(25, [{EPOLLIN, {u32=9644768, u64=9644768}}], 512, 60000) = 1
recvfrom(28, "GET / HTTP/1.1\r\nUser-Agent: curl"..., 1024, 0, NULL, NULL) = 78
stat("/data/web/html/index.html", {st_mode=S_IFREG|0644, st_size=612, ...}) = 0
open("/data/web/html/index.html", O_RDONLY|O_NONBLOCK) = 29
fstat(29, {st_mode=S_IFREG|0644, st_size=612, ...}) = 0
writev(28, [{"HTTP/1.1 200 OK\r\nServer: nginx/1"..., 238}], 1) = 238
sendfile(28, 29, [0] => [612], 612)     = 612
write(4, "127.0.0.1 - - [18/Jul/2019:19:17"..., 159) = 159
close(29)                               = 0
setsockopt(28, SOL_TCP, TCP_NODELAY, [1], 4) = 0
epoll_wait(25, [{EPOLLIN|EPOLLRDHUP, {u32=9644768, u64=9644768}}], 512, 65000) = 1
recvfrom(28, "", 1024, 0, NULL, NULL)   = 0
close(28)                               = 0
epoll_wait(25, [{EPOLLIN, {u32=9644528, u64=9644528}}], 512, -1) = 1
recvmsg(24, {msg_name(0)=NULL, msg_iov(1)=[{"\2\0\0\0\0\0\0\0\4(\0\0\0\0\0\0\2\0\0\0\0\0\0\0\377\377\377\377\0\0\0\0", 32}], msg_contr
ollen=0, msg_flags=0}, 0) = 32close(27)                               = 0
recvmsg(24, 0x7ffd793995a0, 0)          = -1 EAGAIN (Resource temporarily unavailable)

  修改配置启用open_file_cache

[root@python ~]# cat /data/web/conf/vhast/open.conf 
server {
	listen 8092;
	root html;
	location /{
		open_file_cache max=10 inactive=60s;
		open_file_cache_min_uses 1;
		open_file_cache_valid 60s;
		open_file_cache_errors on;

  测试

[root@python vhast]# ps -ef | grep nginx
root      10241      1  0 19:16 ?        00:00:00 nginx: master process nginx
nginx     10278  10241  0 19:23 ?        00:00:00 nginx: worker process
nginx     10279  10241  0 19:23 ?        00:00:00 nginx: cache manager process
root      10282   7257  0 19:23 pts/2    00:00:00 grep --color=auto nginx
[root@python vhast]# strace -p 10278
strace: Process 10278 attached
epoll_wait(6, [{EPOLLIN, {u32=10106432, u64=10106432}}], 512, -1) = 1
accept4(20, {sa_family=AF_INET, sin_port=htons(36976), sin_addr=inet_addr("127.0.0.1")}, [16], SOCK_NONBLOCK) = 3
epoll_ctl(6, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLRDHUP|EPOLLET, {u32=10107632, u64=10107632}}) = 0
epoll_wait(6, [{EPOLLIN, {u32=10107632, u64=10107632}}], 512, 60000) = 1
recvfrom(3, "GET / HTTP/1.1\r\nUser-Agent: curl"..., 1024, 0, NULL, NULL) = 78
open("/data/web/html/index.html", O_RDONLY|O_NONBLOCK) = 8主意只打一次
fstat(8, {st_mode=S_IFREG|0644, st_size=612, ...}) = 0
writev(3, [{"HTTP/1.1 200 OK\r\nServer: nginx/1"..., 238}], 1) = 238
sendfile(3, 8, [0] => [612], 612)       = 612
write(27, "127.0.0.1 - - [18/Jul/2019:19:25"..., 159) = 159
setsockopt(3, SOL_TCP, TCP_NODELAY, [1], 4) = 0
epoll_wait(6, [{EPOLLIN|EPOLLRDHUP, {u32=10107632, u64=10107632}}], 512, 65000) = 1
recvfrom(3, "", 1024, 0, NULL, NULL)    = 0
close(3)                                = 0
epoll_wait(6, [{EPOLLIN, {u32=10106432, u64=10106432}}], 512, -1) = 1
accept4(20, {sa_family=AF_INET, sin_port=htons(36978), sin_addr=inet_addr("127.0.0.1")}, [16], SOCK_NONBLOCK) = 3
epoll_ctl(6, EPOLL_CTL_ADD, 3, {EPOLLIN|EPOLLRDHUP|EPOLLET, {u32=10107633, u64=10107633}}) = 0
epoll_wait(6, [{EPOLLIN, {u32=10107633, u64=10107633}}], 512, 60000) = 1
recvfrom(3, "GET / HTTP/1.1\r\nUser-Agent: curl"..., 1024, 0, NULL, NULL) = 78
writev(3, [{"HTTP/1.1 200 OK\r\nServer: nginx/1"..., 238}], 1) = 238
sendfile(3, 8, [0] => [612], 612)       = 612
write(27, "127.0.0.1 - - [18/Jul/2019:19:25"..., 159) = 159
setsockopt(3, SOL_TCP, TCP_NODELAY, [1], 4) = 0
epoll_wait(6, [{EPOLLIN|EPOLLRDHUP, {u32=10107633, u64=10107633}}], 512, 65000) = 1
recvfrom(3, "", 1024, 0, NULL, NULL)    = 0
close(3)                                = 0
epoll_wait(6, ^Cstrace: Process 10278 detached
 <detached ...>

  

 

posted @ 2019-07-18 19:32  烟雨楼台,行云流水  阅读(959)  评论(0编辑  收藏  举报