1 [root@proxy ~]# vi /etc/nginx/conf.d/reverse.conf 2 server{ 3 resolver 8.8.8.8; #配置DNS解析IP地址 4 resolver_timeout 30s; #超時(shí)時(shí)間(5秒) 5 listen 8080; 6 access_log /var/log/nginx/reverse.access.log main; 7 error_log /var/log/nginx/reverse.error.log warn; 8 location / { 9 proxy_pass http://$http_host$request_uri; #配置正向代理參數(shù) 10 proxy_set_header Host $http_host; #解決如果URL中帶"."后Nginx 503錯(cuò)誤 11 proxy_buffers 256 4k; #配置緩存大小 12 proxy_max_temp_file_size 0; #關(guān)閉磁盤緩存讀寫減少I/O 13 proxy_connect_timeout 30; #代理連接超時(shí)時(shí)間 14 proxy_cache_valid 200 302 10m; 15 proxy_cache_valid 301 1h; 16 proxy_cache_valid any 1m; #配置代理服務(wù)器緩存時(shí)間 17 } 18 }
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件
[root@proxy ~]# vi /etc/nginx/conf.d/forward.conf server { listen 80; server_name forward.linuxds.com; access_log /var/log/nginx/forward.access.log main; error_log /var/log/nginx/forward.error.log warn; location / { proxy_pass https://www.landiannews.com/; 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; client_max_body_size 10m; #允許客戶端請(qǐng)求的最大單文件字節(jié)數(shù) client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù) proxy_connect_timeout 300; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí)) proxy_send_timeout 300; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí)) proxy_read_timeout 300; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí)) proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳 } }
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件
1 [root@nginx02 ~]# mkdir /usr/share/nginx/rs/ 2 [root@nginx02 ~]# echo '<h1>Rs172.24.10.22-81</h1>' > /usr/share/nginx/rs/index.html 3 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF 4 server { 5 listen 81; 6 server_name 172.24.10.22; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/rs/ 2 [root@nginx03 ~]# echo '<h1>Rs172.24.10.23-81</h1>' > /usr/share/nginx/rs/index.html 3 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF 4 server { 5 listen 81; 6 server_name 172.24.10.23; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件
1 [root@nginx01 ~]# cat >> /etc/nginx/nginx.conf << EOF 2 stream { 3 upstream myweb { 4 server 172.24.10.22:81 weight=5 max_fails=1 fail_timeout=10s; 5 server 172.24.10.23:81; 6 } 7 upstream myssh { 8 hash $remote_addr consistent; 9 server 172.24.10.24:22; 10 } 11 12 server { 13 listen 8888; 14 proxy_connect_timeout 2s; 15 proxy_timeout 900s; 16 proxy_pass myweb; 17 } 18 19 server { 20 listen 2222; 21 proxy_connect_timeout 2s; 22 proxy_timeout 900s; 23 proxy_pass myssh; 24 } 25 } 26 EOF
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件
1 [root@nginx02 ~]# mkdir /usr/share/nginx/basebrowser/ 2 [root@nginx02 ~]# echo '<h1>Chrmoe-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index01.html 3 [root@nginx02 ~]# echo '<h1>Firefox-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index02.html
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/basebrowser.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.22; 5 location / { 6 root /usr/share/nginx/basebrowser; 7 index index01.html; 8 access_log /var/log/nginx/chrmoebrowser.access.log main; 9 error_log /var/log/nginx/chrmoebrowser.error.log warn; 10 } 11 } 12 server { 13 listen 89; 14 server_name 172.24.10.22; 15 location / { 16 root /usr/share/nginx/basebrowser; 17 index index02.html; 18 access_log /var/log/nginx/firefoxbrowser.access.log main; 19 error_log /var/log/nginx/firefoxbrowser.error.log warn; 20 } 21 } 22 EOF
1 [root@nginx03 ~]# mkdir /usr/share/nginx/cellphone/ 2 [root@nginx03 ~]# echo '<h1>iPhone-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index01.html 3 [root@nginx03 ~]# echo '<h1>Android-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index02.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cellphone.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.23; 5 location / { 6 root /usr/share/nginx/cellphone; 7 index index01.html; 8 access_log /var/log/nginx/iphone.access.log main; 9 error_log /var/log/nginx/iphone.error.log warn; 10 } 11 } 12 server { 13 listen 89; 14 server_name 172.24.10.23; 15 location / { 16 root /usr/share/nginx/cellphone; 17 index index02.html; 18 access_log /var/log/nginx/android.access.log main; 19 error_log /var/log/nginx/android.error.log warn; 20 } 21 } 22 EOF
1 [root@nginx04 ~]# mkdir /usr/share/nginx/iebrowser/ 2 [root@nginx04 ~]# echo '<h1>IE Browser-172.24.10.24</h1>' > /usr/share/nginx/iebrowser/index.html
1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/iebrowser.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.24; 5 location / { 6 root /usr/share/nginx/iebrowser; 7 index index.html; 8 access_log /var/log/nginx/iebrowser.access.log main; 9 error_log /var/log/nginx/iebrowser.error.log warn; 10 } 11 } 12 EOF
1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件 3 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 4 [root@nginx03 ~]# nginx -s reload #重載配置文件 5 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx04 ~]# nginx -s reload #重載配置文件
1 [root@client ~]# curl 172.24.10.22:89 2 <h1>Firefox-172.24.10.22</h1> 3 [root@client ~]# 4 [root@client ~]# curl 172.24.10.22:88 5 <h1>Chrmoe-172.24.10.22</h1> 6 [root@client ~]# curl 172.24.10.22:89 7 <h1>Firefox-172.24.10.22</h1> 8 [root@client ~]# curl 172.24.10.23:88 9 <h1>iPhone-172.24.10.23</h1> 10 [root@client ~]# curl 172.24.10.23:89 11 <h1>Android-172.24.10.23</h1> 12 [root@client ~]# curl 172.24.10.24:88 13 <h1>IE Browser-172.24.10.24</h1>
1 [root@nginx01 ~]# mkdir /usr/share/nginx/type/ 2 [root@nginx01 ~]# echo '<h1>Type-172.24.10.21</h1>' > /usr/share/nginx/type/index.html
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/type.conf 2 upstream chrome { 3 server 172.24.10.22:88; 4 } 5 upstream firefox { 6 server 172.24.10.22:89; 7 } 8 upstream iphone { 9 server 172.24.10.23:88; 10 } 11 upstream android { 12 server 172.24.10.23:89; 13 } 14 upstream iebrowser { 15 server 172.24.10.24:88; 16 } 17 18 server { 19 listen 80; 20 server_name type.linuxds.com; 21 access_log /var/log/nginx/type.access.log main; 22 error_log /var/log/nginx/type.error.log warn; 23 proxy_set_header Host $host; 24 proxy_set_header X-Real-IP $remote_addr; 25 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 26 client_max_body_size 10m; #允許客戶端請(qǐng)求的最大單文件字節(jié)數(shù) 27 client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請(qǐng)求的最大字節(jié)數(shù) 28 proxy_connect_timeout 300; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí)) 29 proxy_send_timeout 300; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí)) 30 proxy_read_timeout 300; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí)) 31 proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 32 proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 33 proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大?。╬roxy_buffers*2) 34 proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳 35 location / { 36 root /usr/share/nginx/type/; 37 index index.html; 38 39 if ($http_user_agent ~* "chrome"){ 40 proxy_pass http://chrome; 41 } 42 if ($http_user_agent ~* "firefox"){ 43 proxy_pass http://firefox; 44 } 45 if ($http_user_agent ~* "android"){ 46 proxy_pass http://android; 47 } 48 if ($http_user_agent ~* "iphone"){ 49 proxy_pass http://iphone; 50 } 51 if ($http_user_agent ~* "MSIE"){ 52 proxy_pass http://iebrowser; 53 } 54 } 55 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx01 ~]# nginx -s reload #重載配置文件
1 [root@nginx02 ~]# mkdir /usr/share/nginx/cache/ 2 [root@nginx02 ~]# echo '<h1>Cache-172.24.10.22</h1>' > /usr/share/nginx/cache/index.html 3
1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF 2 server { 3 listen 90; 4 server_name 172.24.10.22; 5 location / { 6 root /usr/share/nginx/cache; 7 index index.html; 8 access_log /var/log/nginx/cache.access.log main; 9 error_log /var/log/nginx/cache.error.log warn; 10 } 11 } 12 EOF
1 [root@nginx02 ~]# ll /usr/share/nginx/cache 2 total 16K 3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html 4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上傳一張測(cè)試圖片 5 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx02 ~]# nginx -s reload #重載配置文件
1 [root@nginx03 ~]# mkdir /usr/share/nginx/cache/ 2 [root@nginx03 ~]# echo '<h1>Cache-172.24.10.23</h1>' > /usr/share/nginx/cache/index.html
1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF 2 server { 3 listen 90; 4 server_name 172.24.10.23; 5 location / { 6 root /usr/share/nginx/cache; 7 index index.html; 8 access_log /var/log/nginx/cache.access.log main; 9 error_log /var/log/nginx/cache.error.log warn; 10 } 11 } 12 EOF
1 [root@nginx03 ~]# ll /usr/share/nginx/cache 2 total 16K 3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html 4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上傳一張測(cè)試圖片 5 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx03 ~]# nginx -s reload #重載配置文件
1 [root@nginx01 ~]# mkdir -p /data/cache #創(chuàng)建緩存目錄 2 [root@nginx01 ~]# mkdir -p /data/cache_temp #創(chuàng)建緩存臨時(shí)目錄
1 [root@nginx01 ~]# vi /etc/nginx/nginx.conf #追加如下代碼日志記錄 2 …… 3 log_format main '[$remote_addr]-[$remote_user]-[$time_local]-["$request"]' 4 '[$status]-[$body_bytes_sent]-["$http_referer"]' 5 '["$http_user_agent"]-["$http_x_forwarded_for"]'; 6 '[$upstream_addr]-[$upstream_status]-[$request_time]-[$upstream_response_time]' 7 ……
1 [root@nginx01 ~]# vi /etc/nginx/conf.d/cache.conf 2 upstream cache { 3 server 172.24.10.22:90; 4 server 172.24.10.23:90; 5 } 6 7 proxy_temp_path /data/cache_temp; 8 proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:20m max_size=10g inactive=60m use_temp_path=off; 9 10 server { 11 listen 80; 12 server_name cache.linuxds.com; 13 access_log /var/log/nginx/cache.access.log main; 14 error_log /var/log/nginx/cache.error.log warn; 15 16 location / { 17 expires 3d; 18 add_header Nginx-Cache "$upstream_cache_status"; #增加一個(gè)頭信息 19 20 proxy_cache mycache; #調(diào)用定義的cache zone 21 proxy_pass http://cache; #配置反向代理 22 proxy_set_header Host $host; 23 proxy_set_header X-Real-IP $remote_addr; 24 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 25 26 proxy_cache_valid 200 302 304 2h; #200和302及304頭信息過期時(shí)間為2小時(shí) 27 proxy_cache_valid any 10m; #其他過期時(shí)間10分鐘 28 proxy_cache_key $host$request_uri$uri$is_args$args; #定義緩存的key 29 proxy_cache_bypass $cookie_nocache $arg_comment; #配置不緩存 30 proxy_no_cache $arg_nocache; #配置不緩存 31 proxy_cache_lock on; 32 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; #一個(gè)服務(wù)報(bào)錯(cuò)請(qǐng)求下一個(gè) 33 } 34 location ~ /purge(/.*) { 35 allow 127.0.0.1; 36 allow 172.24.10.0/24; 37 deny all; 38 proxy_cache_purge mycache $1$is_args$args; 39 } 40 }
1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx01 ~]# nginx -s reload #重載配置文件
1 [root@client ~]# curl -I http://cache.linuxds.com/nginx.jpg
聯(lián)系客服