- 环境:
- nginx 反向代理 172.16.0.100
- apache1 web 172.16.0.110
- apache2 web 172.16.0.120
- 目的:网站静态文件分离为多个域名,每个域名分别在两台apache上做负载均衡,nginx缓存静态文件,并实现故障转移……
- --------------------------------------------
- nginx配置:
- vim /usr/local/nginx/conf/proxy.conf
- proxy_redirect off;
- 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_ignore_client_abort on;
- proxy_connect_timeout 90;
- proxy_send_timeout 90; proxy_read_timeout 90;
- proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
- proxy_set_header Accept-Encoding '';
- vim /usr/local/nginx/conf/nginx.conf
- server{
- listen 80;
- server_name jpg.test.com;
- root /data/webroot/jpg;
- location ~* .*\.(jpg|gif|js|css|html|htm)$ {
- expires 1d;
- proxy_store on;
- proxy_store_access user:rw group:r all:r;
- proxy_temp_path /data/webroot/jpg;
- include proxy.conf;
- if ( !-e $request_filename) {
- proxy_pass http://jpg;}}
- }
- server{
- listen 80;
- server_name jpg1.test.com;
- root /data/webroot/jpg1;
- location ~* .*\.(jpg|gif|js|css|html|htm)$ {
- expires 1d;
- proxy_store on;
- proxy_store_access user:rw group:r all:r;
- proxy_temp_path /data/webroot/jpg1;
- include proxy.conf;
- if ( !-e $request_filename) {
- proxy_pass http://jpg1;}}
- }
- server{
- listen 80;
- server_name jpg2.test.com;
- root /data/webroot/jpg2;
- location ~* .*\.(jpg|gif|js|css|html|htm)$ {
- expires 1d;
- proxy_store on;
- proxy_store_access user:rw group:r all:r;
- proxy_temp_path /data/webroot/jpg2;
- include proxy.conf;
- if ( !-e $request_filename) {
- proxy_pass http://jpg2;}}
- }
- server{
- listen 80;
- server_name jpg3.test.com;
- root /data/webroot/jpg3;
- location ~* .*\.(jpg|gif|js|css|html|htm)$ {
- expires 1d;
- proxy_store on;
- proxy_store_access user:rw group:r all:r;
- proxy_temp_path /data/webroot/jpg3;
- include proxy.conf;
- if ( !-e $request_filename) {
- proxy_pass http://jpg3;}}
- }
- upstream jpg {
- server 172.16.0.110:80 max_fails=3 fail_timeout=30s;
- server 172.16.0.120:80 max_fails=3 fail_timeout=30s;
- }
- upstream jpg1 {
- server 172.16.0.110:81 max_fails=3 fail_timeout=30s;
- server 172.16.0.120:81 max_fails=3 fail_timeout=30s;
- }
- upstream jpg2 {
- server 172.16.0.110:82 max_fails=3 fail_timeout=30s;
- server 172.16.0.120:82 max_fails=3 fail_timeout=30s;
- }
- upstream jpg3 {
- server 172.16.0.110:83 max_fails=3 fail_timeout=30s;
- server 172.16.0.120:83 max_fails=3 fail_timeout=30s;
- }
- ---------------------------------------------
- apache都使用以下配置:注意使用端口区分!!!
- <VirtualHost *:80>
- ServerAdmin webmaster@test.cn
- DocumentRoot "/data/webroot/jpg"
- ServerName jpg.test.com
- </VirtualHost>
- <VirtualHost *:81>
- ServerAdmin webmaster@test.cn
- DocumentRoot "/data/webroot/jpg"
- ServerName jpg1.test.com
- </VirtualHost>
- <VirtualHost *:82>
- ServerAdmin webmaster@test.cn
- DocumentRoot "/data/webroot/jpg2"
- ServerName jpg2.test.com
- </VirtualHost>
- <VirtualHost *:83>
- ServerAdmin webmaster@test.cn
- DocumentRoot "/data/webroot/jpg3"
- ServerName jpg3.test.com
- </VirtualHost>
- -------------------------------------------------------------------------------------------
- 总结:
- 1、防盗链
- 如果apache使用了图片防盗链,则nginx不用再做防盗链配置
- 2、apache
- if ( !-e $request_filename) {
- proxy_pass http://172.16.0.110:83;}
- 还可以改/etc/hosts文件,解析域名到对应后端web服务器。建议使用端口方式!
- 4、关于proxy_set_header Accept-Encoding '';
- nginx 向后端请求 gzip 内容
- proxy_set_header Accept-Encoding 'gzip';
- nginx 向后端请求未 gzip 内容
- proxy_set_header Accept-Encoding '';
- 5、nginx定义服务器集时最好使用端口定义不同服务器
- 6、 proxy_temp_path定义的目录nginx为了有写入权限,会在重启nginx时自动把此目录属主改为nginx进程用户
- 7、proxy_next_upstream设定了相应条件后,nginx只查寻一遍,不会无限制查询