nginx (1)

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

wKioL1Y4TWeR1K11AAJ8P4lSpjE450.jpg

[root@localhost ~]# rpm -ql nginx

wKiom1Y4Ta-TzpXkAAJ2x5yF4Dk841.jpg

[root@localhost ~]# service nginx start

正在启动 nginx:                                           [确定]

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

LISTEN     0      128                       *:80                       *:*    

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

wKioL1Y4UDWDqaoeAAG6SEZo-gw425.jpg


实例

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

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

<h1>httpd on node3</h1>

[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

<h1>httpd on node3</h1>


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/;

[root@node1 conf.d]# /etc/init.d/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:                                           [确定]

wKioL1Y9NAijyO8iAACER2R-Usg092.jpg



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 <h1>bbs page</h1>


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:                                           [确定]

wKiom1Y9N6jhwiGbAAB8IpUtlNA135.jpg

wKioL1Y9N-uj_c8SAAGB21oOyEU961.jpg


 wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

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

 13     location /forum {

 14         proxy_pass http://192.168.204.140/bbs;

 15    }

wKiom1Y9OJazimm9AAB4wUsrPAU820.jpg


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:                                           [确定]

wKioL1Y9OZ7zkDEGAAB8Sg71c4o029.jpg


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

wKiom1Y9QTTz3VkJAACpKdGi27U062.jpg

wKioL1Y9QXay6l7pAACO1Apbw3w025.jpg

wKiom1Y9QTTTDb2YAAF8kAHYDUI427.jpg

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# ls

1.jpg  2.png  bbs  index.html

[root@node3 html]# mkdir images

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

[root@node3 html]# ls

1.jpg  bbs  images  index.html

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

重新载入 nginx:                                           [确定]

wKioL1Y9Qr6TNLt5AACzvR6STQM944.jpg


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

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

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

 17           proxy_pass http://192.168.204.140/images;

 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

注意:模式匹配后面不能跟上/images,只能是proxy_pass http://192.168.204.140;


$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:   

wKioL1Y9RtWCjkNOAACspSqgW3g050.jpg

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

wKioL1Y9RtWCjkNOAACspSqgW3g050.jpg

[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

wKiom1Y9qwmChP95AACbtfYdo0o375.jpg

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

f7c2c5a71a29e73cf166505389455827


wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# vim index.html 

  1 <h1>httpd on node3 new</h1>

wKiom1Y9qwmChP95AACbtfYdo0o375.jpg


[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 *

wKiom1Y9rJiRtPe4AAClWjnAMbo751.jpg



定义连接超时时间、默认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_read_timeout time;

Default: proxy_read_timeout 60s;

Context: http, server, location

Defines a timeout for reading a response from 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

<h1>nginx on node20 204.120</h1>    

[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 {

 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 htt    p_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:                                           [确定]

wKiom1Y9vPySa3T0AAEr0pW7iII489.jpg


wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg

[root@node1 nginx]# vim nginx.conf

 25      upstream upservers {

 26         server 192.168.204.140 weight=2;

 27         server 192.168.204.120;

 28         }

 [root@node1 nginx]# service nginx reload

重新载入 nginx:                                           [确定]

wKiom1Y9vizx615qAAHG35GdlZ4935.jpg 


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:                                           [确定]

wKioL1Y9v4CjlG5UAAG9-7aNq44850.jpg   

                               

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

wKioL1Y9wW_g5acMAAC22Qt6KzI341.jpg


wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# service httpd start

正在启动 httpd:                                           [确定]

wKiom1Y9wjLwNBqKAAE4Mhd2jpU703.jpg


通过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:          [确定]

wKioL1Y9xCfw0mx_AAC39kpn3uc355.jpg

wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg

[root@node3 html]# service httpd stop

停止 httpd:                                               [确定]

wKiom1Y9xLyhCd3mAAC2aWkEanI247.jpg

[root@node3 html]# service httpd start

正在启动 httpd:                                           [确定]

wKioL1Y9xU6Qs0phAACknVdEgZU142.jpg


你可能感兴趣的:(nginx)