1、实现nginx URL重写,实例域名跳转
- # vim /etc/nginx/nginx.conf #实例如下
- server {
- listen 80;
- server_name www.linuxidc.net;
- root html;
- index index.html index.htm;
- rewrite ^/ http://www.linuxidc.com/;
- }
- # service nginx restart
说明:在windows下当你访问http://www.linuxidc.net/的时候,自动跳转到http://www.linuxidc.com/的服务上了。
2、.实现反向代理
- # vim /etc/nginx/nginx.conf
- server {
- listen 80;
- server_name www.linuxidc.net;
- root html;
- index index.html index.htm;
- proxy_pass http://www.linuxidc.com;
- }
- # service nginx restart
说明:当你访问www.linuxidc.net的服务时,此时www.linuxidc.net并没有提供web服务,而是反向代理到www.linuxidc.com的web服务上了。
3、实现缓存服务器
- # vim /etc/nginx/nginx.conf
- http {
- proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
- server {
- location / {
- proxy_pass http:// www.linuxidc.com;
- proxy_set_header Host $host;
- proxy_cache STATIC;
- proxy_cache_valid 200 302 10m;
- proxy_cache_valid 301 1h;
- proxy_cache_valid any 1m;
- proxy_cache_use_stale error timeout invalid_header updating
- http_500 http_502 http_503 http_504;
- }
- }
- }
- # service nginx restart
说明:当你访问www.linuxidc.net反向代理到www.linuxidc.com的时候,明显比直接访问www.linuxidc.com的速度快,这就是缓存服务的作用。
4、做负载均衡
- # vim /etc/nginx/nginx.conf
- upstream loadbalance {
- server www.linuxidc.com weight=5;
- server www.lhlinuxidc.com;
- }
- server {
- listen 80;
- server_name www.linuxidc.net;
- location / {
- include proxy.conf;
- proxy_pass http:// loadbalance;
- }
- }
- # service nginx restart
说明:在windows主机上当你访问www.linuxidc.net的时候,刷新几次会是不同的页面,这就说明负载均衡实现了(如果想明确看到实验结果,可以让web1和web2的页面不一样,但实际工作中,二者的web页面数据是完全一致的)
5、实现健康状态监测
- # vim /etc/nginx/nginx.conf
- http {
- upstream loadba {
- server 172.16.22.2:80;
- server 172.16.22.3:80;
- healthcheck_enabled;
- healthcheck_delay 1000;
- healthcheck_timeout 1000;
- healthcheck_failcount 1;
- healthcheck_send "GET /.health HTTP/1.0";
- }
- server {
- listen 80;
- location / {
- proxy_set_header Host $http_host;
- proxy_pass http://172.16.22.2;
- proxy_connect_timeout 3;
- }
- location /stat {
- healthcheck_status;
- }
- }
- }
- # service nginx restart
说明:当你在地址栏里输入:http://172.16.22.1/stat,即可看到web1和web2 web服务的健康状况的