(1).正向代理的概念
正向代理:是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),然后代理向原始服务器转交请求并将获得的内容返回给客户端。代理的是客户端,客户端必须要进行一些特别的设置才能使用正向代理。
(2).反向代理的概念
1
2
3
4
5
|
location
/bbs
{
11 root
/www/c
.com/;
12 index index.html index.htm;
#179为后端一台httpd服务器,本机180
13 proxy_pass
#这里一定要带“/”
14 }
|
1
2
3
|
location ~ ^
/bbs
{
proxy_pass
#此时这里后面不能带“/”否则会报错
}
|
1
2
3
4
|
location / {
rewrite /(.*)$
/index
.php?page=$1
break
;
proxy_pass http:
//localhost
:8080
/index
;
}
|
1
2
3
4
5
6
7
8
9
|
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
#后端Http服务器记录日志时获取真是客户ip时在Nginx代理服务器上的设置,后端服务器也要做相应的设置
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#多次nginx转发时使用的?
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 30;
proxy_send_timeout 15;
proxy_read_timeout 15;
|
1
2
3
4
|
42 upstream webservers(自定义的组名字) {
43 server 192.168.100.179 weight=2;
44 server 192.168.100.175;
45 }
|
1
2
3
4
5
|
20 location / {
21
#root /usr/share/nginx/html;
22 proxy_pass http:
//webservers/
;
#这里使用之前定义的组名,要加http://,组名后记得加"/"
23 index index.html index.htm;
24 }
|
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
|
upstream webservers {
server 192.168.100.179 weight=3 max_fails=2 fail_timeout=3 down;
server 192.168.100.175 weight=1 max_fails=2 fail_timeout=3;
server 127.0.0.1:8080 backup;
least_conn;
}
server {
listen 80;
# listen somename:8080;
# server_name alias another.alias;
server_name www.c.com c.com;
location / {
root
/www/c
.com/;
index index.html index.htm;
proxy_pass http:
//webservers/
;
proxy_set_header X-Real-IP $remote_addr;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
}
server {
listen 8080;
server_name 127.0.0.1;
root
/www/backup/
;
[root@BAIYU_180 nginx]
# mkdir /www/backup
[root@BAIYU_180 nginx]
# vi /www/backup/index.html
1 sorry...
|
-
轮询(默认)。每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。Weight 指定轮询权值,Weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。
-
ip_hash。每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。
相当于lvs的sh算法,用来实现session绑定,与server address 一起使用
-
fair(第三方)。这是比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。
-
url_hash(第三方)。此方法按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包。
1
2
3
4
5
6
|
upstream backend {
server backend1.example.com;
server backend2.example.com;
sticky cookie srv_id expires=1h domain=.example.com path=/;
} 名称 过期时间 访问路径
|
1
2
3
|
proxy_cache_path
/data/nginx/cache/one
levels=1 keys_zone=one:10m;
proxy_cache_path
/data/nginx/cache/two
levels=2:2 keys_zone=two:100m;
proxy_cache_path
/data/nginx/cache/three
levels=1:1:2 keys_zone=three:1000m;
|
1
2
|
proxy_cache_bypass $cookie_nocache $arg_nocache $arg_comment;
proxy_cache_bypass $http_pragma $http_authorization;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
http {
proxy_cache_path
/data/nginx/cache
levels=1:2 keys_zone=STATIC:10m
inactive=24h max_size=1g;
server {
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
location / {
proxy_pass http:
//www
.magedu.com;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_valid 301 302 10m;
proxy_cache_vaild any 1m;
proxy_cache_use_stale error timeout invalid_header updating
http_500 http_502 http_503 http_504;
}
}
}
|
1
2
3
4
5
6
|
location / {
proxy_pass http:
//192
.168.100.179;
}
location ~* \.(jpg|jpeg|png|gif)$ {
proxy_pass http:
//192
.168.100.175;
}
|
1
2
3
4
5
6
|
location / {
proxy_pass http:
//192
.168.100.179;
}
location ~* \.php$ {
fastcgi_pass http:
//xxx
;
}
|
1
|
[root@BAIYU_180 ~]
# yum install php-fpm php-mysql mysql-server mysql nginx
|
1
2
3
4
5
6
7
|
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;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
fastcgi_param GATEWAY_INTERFACE CGI
/1
.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
|
1
2
3
4
|
location / {
root html;
index index.php index.html index.htm;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[root@BAIYU_180 php-fpm.d]
# service php-fpm start
正在启动 php-fpm:[确定]
[root@BAIYU_180 php-fpm.d]
# netstat -nlptu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID
/Program
name
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 27487
/nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27487
/nginx
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1338
/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1172
/master
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 30106
/php-fpm
[root@BAIYU_180 php-fpm.d]
# service nginx configtest
nginx: the configuration
file
/etc/nginx/nginx
.conf syntax is ok
nginx: configuration
file
/etc/nginx/nginx
.conf
test
is successful
[root@BAIYU_180 php-fpm.d]
# service nginx start
正在启动 nginx:[确定]
[root@BAIYU_180 php-fpm.d]
# service mysql start
[root@BAIYU_180 nginx]
# cd /usr/share/nginx/html/
[root@BAIYU_180 html]
# ls
404.html 50x.html index.html nginx-logo.png poweredby.png
[root@BAIYU_180 html]
# vi index.php
1 <?php phpinfo(); ?>
|
1
2
3
4
5
6
7
8
9
10
11
|
location ~ \.php$ {
84 root
/www/a
.com/;
85 fastcgi_pass 127.0.0.1:9000;
86 fastcgi_index index.php;
87 fastcgi_param SCRIPT_FILENAME
/scripts
$fastcgi_script_name;
88 include fastcgi_params;
89 fastcgi_cache fcgi;
90 fastcgi_cache_valid 200 1h;
91 fastcgi_cache_valid 301 302 5m;
92 fastcgi_cache_valid any 1m;
93 }
|
1
2
3
4
5
6
7
8
9
|
http {
limit_zone first $binary_remote_addr 10m;
server {
location
/downloads/
{
limit_conn first 1;
limit_rate 50k;
}
}
}
|
1
2
3
4
5
6
7
|
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server {
...
limit_conn perip 10;
limit_conn perserver 100;
}
|
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
|
user nobody nobody;
worker_processes 4;
worker_rlimit_nofile 51200;
error_log logs
/error
.log notice;
pid
/var/run/nginx
.pid;
events {
use epoll;
worker_connections 51200;
}
http {
server_tokens off;
#关闭软件版本信息
include mime.types;
proxy_redirect off;
proxy_set_header Host $host;
#http请求报文中host首部;如果请求中没有host首部,则以处理此请求的主机的主机名代替
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#记录多级代理的ip?
client_max_body_size 20m;
#客户端单个大小最大为20m
client_body_buffer_size 256k;
#内存中缓存的大小
proxy_connect_timeout 90;
#连接后端服务器超时时间
proxy_send_timeout 90;
#后端服务器发送响应报文超时时间
proxy_read_timeout 90;
#读取...
proxy_buffer_size 128k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
default_type application
/octet-stream
;
charset utf-8;
client_body_temp_path
/var/tmp/client_body_temp
1 2;
proxy_temp_path
/var/tmp/proxy_temp
1 2;
fastcgi_temp_path
/var/tmp/fastcgi_temp
1 2;
uwsgi_temp_path
/var/tmp/uwsgi_temp
1 2;
scgi_temp_path
/var/tmp/scgi_temp
1 2;
ignore_invalid_headers on;
server_names_hash_max_size 256;
server_names_hash_bucket_size 64;
client_header_buffer_size 8k;
large_client_header_buffers 4 32k;
connection_pool_size 256;
request_pool_size 64k;
output_buffers 2 128k;
postpone_output 1460;
client_header_timeout 1m;
client_body_timeout 3m;
send_timeout 3m;
log_format main
'$server_addr $remote_addr [$time_local] $msec+$connection '
'"$request" $status $connection $request_time $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
open_log_file_cache max=1000 inactive=20s min_uses=1 valid=1m;
access_log logs
/access
.log main;
log_not_found on;
sendfile on;
tcp_nodelay on;
tcp_nopush off;
reset_timedout_connection on;
keepalive_timeout 105;
keepalive_requests 100;
gzip
on;
gzip_http_version 1.1;
gzip_vary on;
gzip_proxied any;
gzip_min_length 1024;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_proxied expired no-cache no-store private auth no_last_modified no_etag;
gzip_types text
/plain
application
/x-javascript
text
/css
application
/xml
application
/json
;
gzip_disable
"MSIE [1-6]\.(?!.*SV1)"
;
upstream tomcat8080 {
ip_hash;
server 172.16.100.103:8080 weight=1 max_fails=2;
server 172.16.100.104:8080 weight=1 max_fails=2;
server 172.16.100.105:8080 weight=1 max_fails=2;
}
server {
listen 80;
server_name www.magedu.com;
# config_apps_begin
root
/data/webapps/htdocs
;
access_log
/var/logs/webapp
.access.log main;
error_log
/var/logs/webapp
.error.log notice;
location / {
location ~* ^.*
/favicon
.ico$ {
root
/data/webapps
;
expires 180d;
break
;
}
if
( !-f $request_filename ) {
proxy_pass http:
//tomcat8080
;
break
;
}
}
error_page 500 502 503 504
/50x
.html;
location =
/50x
.html {
root html;
}
}
server {
listen 8088;
server_name nginx_status;
location / {
access_log off;
deny all;
return
503;
}
location
/status
{
stub_status on;
access_log off;
allow 127.0.0.1;
allow 172.16.100.71;
deny all;
}
}
}
|