注意:

location /uri {

proxy_pass http://back_service:port/newuri/;

}

/uri或者/--->/newuri/

模式匹配--->http://back_service:port;

重定向--->http://back_service:port;


nginx(1)


共3台服务器

wKiom1Y9xZCTFuCJAAA-sAMDMCw874.jpg


rpm安装:

wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@localhost ~]# ls

    anaconda-ks.cfg     nginx-1.8.0-1.el6.ngx.x86_64.rpm  视频  下载

    install.log         公共的                            图片  音乐

    install.log.syslog  模板                              文档  桌

[root@localhost ~]# yum install nginx-1.8.0-1.el6.ngx.x86_64.rpm -y

[root@localhost ~]# tail /etc/passwd

nginx (1)_第1张图片

[root@localhost ~]# rpm -ql nginx

nginx (1)_第2张图片

[root@localhost ~]# service nginx start

正在启动 nginx:                                           [确定]

[root@localhost ~]# ss -tanl |grep 80

LISTEN     0      128                       *:80                       *:*    

[root@node1 ~]# firefox http://node1

nginx (1)_第3张图片


实例

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 ~]# vim /var/www/html/index.html

httpd on node3

[root@node3 ~]# vim /var/www/html/

[root@node3 ~]# vim /var/www/html/index.html

[root@node3 ~]# service httpd start

正在启动 httpd:httpd: Could not reliably determine the server's fully qualified domain name, using node3.zye.com for ServerName

[root@node3 ~]# curl  http://node3

httpd on node3


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# pwd

/etc/nginx/conf.d

[root@node1 conf.d]# ls

default.conf  example_ssl.conf

[root@node1 conf.d]# cp default.conf{,.bak}

[root@node1 conf.d]# ls

default.conf  default.conf.bak  example_ssl.conf

[root@node1 conf.d]# vim default.conf    

 8     location / {

 9  #       root   /usr/share/nginx/html;

 10         proxy_pass http://192.168.204.140/;

          index  index.html index.htm;          ##可用可无


[root@node1 conf.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@node1 conf.d]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第4张图片



wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 ~]# cd /var/www/html/

[root@node3 html]# mkdir bbs

[root@node3 html]# cd bbs/

[root@node3 bbs]# vim index.html

  1

bbs page


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

  8     location / {

  9         root   /usr/share/nginx/html;

 10         index  index.html index.htm;

 11     }   

 12     

 13     location /bbs {

 14         proxy_pass http://192.168.204.140/bbs/;

 15    }  

[root@node1 conf.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@node1 conf.d]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第5张图片

nginx (1)_第6张图片


 wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

 13     location /forum {

 14         proxy_pass http://192.168.204.140/bbs/;

 15    }

nginx (1)_第7张图片


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

 13     location /forum {

 14         proxy_pass http://192.168.204.140/;

 15    }

[root@node1 conf.d]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第8张图片


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

 16     location ~* \.(jpg|png|gif)$ {

 17           proxy_pass http://192.168.204.140;

 18      }

[root@node1 conf.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@node1 conf.d]# service nginx reload

重新载入 nginx:                                           [确定]


wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# ls

1.jpg  2.png  bbs  index.html

nginx (1)_第9张图片

nginx (1)_第10张图片

nginx (1)_第11张图片

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# ls

1.jpg  2.png  bbs  index.html

[root@node3 html]# mkdir p_w_picpaths

[root@node3 html]# mv 2.png p_w_picpaths/

[root@node3 html]# ls

1.jpg  bbs  p_w_picpaths  index.html

[root@node1 conf.d]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第12张图片


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

 16     location ~* \.(jpg|png|gif)$ {

 17     proxy_pass http://192.168.204.140/p_w_picpaths;  注意:模式匹配后面只能是http://192.168.204.140;

 18      }

[root@node1 conf.d]# service nginx configtest

nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/conf.d/default.conf:17

nginx: configuration file /etc/nginx/nginx.conf test failed


---------------------------------------------------------------------------------

定义日志

$host

  • in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request

$remote_addr

  • client address

示例

location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}

wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 conf.d]# vim default.conf

  13     location /forum {

 14         proxy_pass http://192.168.204.140/;

 15         proxy_set_header Host $host;

 16         proxy_set_header X-Real-IP $remote_addr;

 17      }

 18     location ~* \.(jpg|png|gif)$ {

 19           proxy_pass http://192.168.204.140;

 20         proxy_set_header X-Real-IP $remote_addr;

 21      }  

[root@node1 conf.d]# vim default.conf

[root@node1 conf.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@node1 conf.d]# service nginx reload

重新载入 nginx:   

nginx (1)_第13张图片

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# !tail

tail -fn 10 /var/log/httpd/access_log 

192.168.204.130 - - [23/Oct/2015:17:55:07 +0800] "GET /bbs/ HTTP/1.0" 200 18 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"

[root@node3 html]# vim /etc/httpd/conf/httpd.conf 

 497 LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \     "%{User-Agent}i\"" combined

nginx (1)_第14张图片

[root@node3 html]# !tail

tail -fn 10 /var/log/httpd/access_log 

192.168.204.1 - - [23/Oct/2015:19:11:24 +0800] "GET // HTTP/1.0" 304 - "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36"

--------------------------------------------------------------------------------------

定义缓存

Syntax:proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

Default:

Context:http

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

:
 

/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c



syntax:	proxy_cache zone | off;
Default:	proxy_cache off;
Context:	http, server, location


管理修剪缓存(后端内容改变,代理端已缓存住时,删除缓存)

Syntax:proxy_cache_purge string ...;

Default:

Context:http, server, location

This directive appeared in version 1.5.7.


proxy_cache_path /data/nginx/cache keys_zone=cache_zone:10m;

map $request_method $purge_method {

    PURGE   1;

    default 0;

}

server {

    ...

    location / {

        proxy_pass http://backend;

        proxy_cache cache_zone;

        proxy_cache_key $uri;

        proxy_cache_purge $purge_method;

    }

}


缓存的过期后向后端服务器是否重新校验

Syntax:proxy_cache_revalidate on | off;

Default:proxy_cache_revalidate off;

Context:http, server, location

This directive appeared in version 1.5.7.


当后端服务器宕机,仍然有缓存,在这种情况下,在什么场合使用过期缓存

Syntax:proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

Default:proxy_cache_use_stale off;

Context:http, server, location


按照响应码,自定义缓存时间

Syntax:proxy_cache_valid [code ...] time;

Default:

Context:http, server, location


proxy_cache_valid 200 302 10m;

proxy_cache_valid 301      1h;

proxy_cache_valid any      1m;




wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 nginx]# vim nginx.conf

 14 http {

 15     include       /etc/nginx/mime.types;

 16     default_type  application/octet-stream;

 17 

 18     log_format  main  '$remote_addr - $remote_user [$time_local] "    $request" '

 19                       '$status $body_bytes_sent "$http_referer" '

 20                       '"$http_user_agent" "$http_x_forwarded_for"'    ;

 21 

 22     access_log  /var/log/nginx/access.log  main;

 23     proxy_cache_path /cache/nginx levels=1:1 keys_zone=mycache:32m    ;


[root@node1 nginx]# mkdir -pv /cache/nginx

mkdir: 已创建目录 "/cache"

mkdir: 已创建目录 "/cache/nginx"

[root@node1 ~]# chown -R nginx.nginx /cache/nginx/


[root@node1 etc]# vim nginx/conf.d/default.conf

 13     location /forum {

 14         proxy_cache_valid 200 1d;

 15         proxy_cache_valid 301 302 10m;

 16         proxy_cache_valid any 1m;

 17         proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

 18         proxy_cache mycache;

 19         proxy_pass http://192.168.204.140/;

 20         proxy_set_header Host $host;

 21         proxy_set_header X-Real-IP $remote_addr;

 22    }

 23     location ~* \.(jpg|png|gif)$ {

 24         proxy_cache_valid 200 1d;

 25         proxy_cache_valid 301 302 10m;

 26         proxy_cache_valid any 1m;

 27        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

 28         proxy_cache mycache;

 30         proxy_pass http://192.168.204.140;

 31         proxy_set_header X-Real-IP $remote_addr;

 32      }

[root@node1 etc]# 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@node1 etc]# service nginx reload

重新载入 nginx:                                           [确定]


[root@node1 etc]# ll /cache/nginx/

总用量 0

nginx (1)_第15张图片

[root@node1 2]# ls /cache/nginx/7/2

f7c2c5a71a29e73cf166505389455827


wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# vim index.html 

  1

httpd on node3 new

nginx (1)_第16张图片


[root@node1 nginx]# cd 7

[root@node1 7]# ls

2

[root@node1 7]# cd 2

[root@node1 2]# ls

f7c2c5a71a29e73cf166505389455827

[root@node1 2]# rm -rf *

nginx (1)_第17张图片


---------------------------------------------------------------------------------


定义连接请求后端服务器超时时间、默认60秒(跟过期缓存有关系)

Syntax:proxy_connect_timeout time;

Default:proxy_connect_timeout 60s;

Context:http, server, location



代理端到客户端指定的nginx首部

Syntax:proxy_hide_header field;

Default:

Context:http, server, location


客户端请求body通过nginx不做封装

Syntax:proxy_pass_request_body on | off;

Default:proxy_pass_request_body on;

Context:http, server, location

Indicates whether the original request body is passed to the proxied server.



客户端头部原始请求发到后端

Syntax:proxy_pass_request_headers on | off;

Default:proxy_pass_request_headers on;

Context:http, server, location

Indicates whether the header fields of the original request are passed to the proxied server.


-------------------------------------------------------------------------------------


wKiom1Y9uDuTNti0AAATTqEQP74758.jpg        

[root@node20 ~]# vim /var/www/html/index.html

nginx on node20 204.120

    

[root@node20 ~]# vim /var/www/html/index.html

[root@node20 ~]# service httpd restart

停止 httpd:                                               [失败]

正在启动 httpd:                                           [确定]

[root@node20 ~]# httpd -t

Syntax OK


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {         ##只能定义在http{..}

 26         server 192.168.204.140;

 27         server 192.168.204.120;

 28         }


[root@node1 nginx]# vim conf.d/default.conf

 13     location /forum/ {

 14         #proxy_cache_valid 200 1d;

 15         #proxy_cache_valid 301 302 10m;

 16         #proxy_cache_valid any 1m;

 17         #proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

 18         #proxy_cache mycache;

 19         proxy_pass http://upservers/;

 20         proxy_set_header Host $host;

 21         proxy_set_header X-Real-IP $remote_addr;

 22    }

 23     location ~* \.(jpg|png|gif)$ {

 24         #proxy_cache_valid 200 1d;

 25         #proxy_cache_valid 301 302 10m;

 26         #proxy_cache_valid any 1m;

 27         #proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

 28         #proxy_cache mycache;

 29         proxy_pass http://upservers;     ##没有“/”

 30         proxy_set_header X-Real-IP $remote_addr;

 31      }


[root@node1 nginx]# 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@node1 nginx]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第18张图片


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {

 26         server 192.168.204.140 weight=2;   ##加权轮询,默认=1;

 27         server 192.168.204.120;  

 28         }

 [root@node1 nginx]# service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第19张图片 


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

ip_hash根据ip的hash调到单一的服务器

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {

 26         ip_hash;           ## 默认:轮询

 27         server 192.168.204.140 weight=2;

 28         server 192.168.204.120;

 29         }

[root@node1 nginx]# !ser

service nginx reload

重新载入 nginx:                                           [确定]

nginx (1)_第20张图片   

                               

wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

ip_hash根据ip的hash调到单一的服务器

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {

 26         server 192.168.204.140 max_fails=2 fail_timeout=1; ##每次失败的超时时间,共几次定义失败,失败移除;

 27         server 192.168.204.120 max_fails=2 fail_timeout=1;

 28         } 

[root@node1 nginx]# 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@node1 nginx]# service nginx reload

重新载入 nginx:                                           [确定]


后端状态检测功能

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# service httpd stop

停止 httpd:                                               [确定]

You have new mail in /var/spool/mail/root

nginx (1)_第21张图片


wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# service httpd start

正在启动 httpd:                                           [确定]

nginx (1)_第22张图片


-------------------------------------------------------------------------------------------


通过backup停止访问,平滑升级,或者可以当备用

wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {

 26         server 192.168.204.140 max_fails=2 fail_timeout=1;

 27         server 192.168.204.120 max_fails=2 fail_timeout=1 backup;

 28         }

[root@node1 nginx]# !se

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@node1 nginx]# service nginx reload

重新载入 nginx:          [确定]

nginx (1)_第23张图片

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# service httpd stop

停止 httpd:                                               [确定]

nginx (1)_第24张图片

[root@node3 html]# service httpd start

正在启动 httpd:                                           [确定]

nginx (1)_第25张图片