lishenghan 发表于 2018-11-10 10:23:48

nginx (1)

  注意:
  location /uri {
  proxy_pass http://back_service:port/newuri/;
  }
  /uri或者/--->/newuri/
  模式匹配--->http://back_service:port;
  重定向--->http://back_service:port;
  nginx(1)
  共3台服务器
http://s3.51cto.com/wyfs02/M02/75/9B/wKiom1Y9xZCTFuCJAAA-sAMDMCw874.jpg
  rpm安装:
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # ls
  anaconda-ks.cfg   nginx-1.8.0-1.el6.ngx.x86_64.rpm视频下载
  install.log         公共的                            图片音乐
  install.log.syslog模板                              文档桌
  #yum install nginx-1.8.0-1.el6.ngx.x86_64.rpm -y
  # tail /etc/passwd
http://s3.51cto.com/wyfs02/M02/75/63/wKioL1Y4TWeR1K11AAJ8P4lSpjE450.jpg
  # rpm -ql nginx
http://s3.51cto.com/wyfs02/M00/75/66/wKiom1Y4Ta-TzpXkAAJ2x5yF4Dk841.jpg
  # service nginx start
  正在启动 nginx:                                           [确定]
  # ss -tanl |grep 80
  LISTEN   0      128                     *:80                     *:*
  # firefox http://node1
http://s3.51cto.com/wyfs02/M01/75/63/wKioL1Y4UDWDqaoeAAG6SEZo-gw425.jpg
  实例
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # vim /var/www/html/index.html
  httpd on node3
  # vim /var/www/html/
  # vim /var/www/html/index.html
  # service httpd start

  正在启动 httpd:httpd: Could not>  # curlhttp://node3
  httpd on node3
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # pwd
  /etc/nginx/conf.d
  # ls
  default.confexample_ssl.conf
  # cp default.conf{,.bak}
  # ls
  default.confdefault.conf.bakexample_ssl.conf
  #vim default.conf
  8   location / {
  9#       root   /usr/share/nginx/html;
  10         proxy_pass http://192.168.204.140/;
  indexindex.html index.htm;      ##可用可无
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful

  # service nginx>  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M02/75/95/wKioL1Y9NAijyO8iAACER2R-Usg092.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # cd /var/www/html/
  # mkdir bbs
  # cd bbs/
  # vim index.html
  1 bbs page
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim default.conf
  8   location / {
  9         root   /usr/share/nginx/html;
  10         indexindex.html index.htm;
  11   }
  12
  13   location /bbs {
  14         proxy_pass http://192.168.204.140/bbs/;
  15    }
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful

  # service nginx>  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M00/75/97/wKiom1Y9N6jhwiGbAAB8IpUtlNA135.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9N-uj_c8SAAGB21oOyEU961.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim default.conf
  13   location /forum {
  14      proxy_pass http://192.168.204.140/bbs/;
  15    }
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9OJazimm9AAB4wUsrPAU820.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim default.conf
  13   location /forum {
  14         proxy_pass http://192.168.204.140/;
  15    }
  # service nginx>
  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M02/75/95/wKioL1Y9OZ7zkDEGAAB8Sg71c4o029.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim default.conf
  16   location ~* \.(jpg|png|gif)$ {
  17          proxy_pass http://192.168.204.140;
  18      }
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  # service nginx>
  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # ls
  1.jpg2.pngbbsindex.html
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9QTTz3VkJAACpKdGi27U062.jpg
http://s3.51cto.com/wyfs02/M02/75/95/wKioL1Y9QXay6l7pAACO1Apbw3w025.jpg
http://s3.51cto.com/wyfs02/M00/75/97/wKiom1Y9QTTTDb2YAAF8kAHYDUI427.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # ls
  1.jpg2.pngbbsindex.html
  # mkdir images
  # mv 2.png images/
  # ls
  1.jpgbbsimagesindex.html
  # service nginx>
  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M01/75/95/wKioL1Y9Qr6TNLt5AACzvR6STQM944.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim default.conf
  16   location ~* \.(jpg|png|gif)$ {
  17   proxy_pass http://192.168.204.140/images;注意:模式匹配后面只能是http://192.168.204.140;
  18      }
  # service nginx configtest
  nginx: "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;
  
}
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # 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      }
  # vim default.conf
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  #service nginx>
  重新载入 nginx:
http://s3.51cto.com/wyfs02/M01/75/95/wKioL1Y9RtWCjkNOAACspSqgW3g050.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # !tail
  tail -fn 10 /var/log/httpd/access_log
  192.168.204.130 - - "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"
  # vim /etc/httpd/conf/httpd.conf
  497 LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \   "%{User-Agent}i\"" combined
http://s3.51cto.com/wyfs02/M01/75/95/wKioL1Y9RtWCjkNOAACspSqgW3g050.jpg
  # !tail
  tail -fn 10 /var/log/httpd/access_log
  192.168.204.1 - - "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 keys_zone=name:size ;
  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 time;
  Default: —
  Context:http, server, location
  proxy_cache_valid 200 302 10m;
  proxy_cache_valid 301      1h;
  proxy_cache_valid any      1m;
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim nginx.conf
  14 http {
  15   include       /etc/nginx/mime.types;
  16   default_typeapplication/octet-stream;
  17
  18   log_formatmain'$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.logmain;
  23   proxy_cache_path /cache/nginx levels=1:1 keys_zone=mycache:32m    ;
  # mkdir -pv /cache/nginx
  mkdir: 已创建目录 "/cache"
  mkdir: 已创建目录 "/cache/nginx"
  # chown -R nginx.nginx /cache/nginx/
  # vim nginx/conf.d/default.conf
  13   location /forum {
  14         proxy_cache_valid 200 1d;
  15proxy_cache_valid 301 302 10m;
  16proxy_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)$ {
  24proxy_cache_valid 200 1d;
  25 proxy_cache_valid 301 302 10m;
  26proxy_cache_valid any 1m;
  27proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
  28proxy_cache mycache;
  30         proxy_pass http://192.168.204.140;
  31         proxy_set_header X-Real-IP $remote_addr;
  32      }
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  # service nginx>
  重新载入 nginx:                                           [确定]
  # ll /cache/nginx/
  总用量 0
http://s3.51cto.com/wyfs02/M02/75/9A/wKiom1Y9qwmChP95AACbtfYdo0o375.jpg
  # ls /cache/nginx/7/2
  f7c2c5a71a29e73cf166505389455827
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # vim index.html
  1 httpd on node3 new
http://s3.51cto.com/wyfs02/M02/75/9A/wKiom1Y9qwmChP95AACbtfYdo0o375.jpg
  # cd 7
  # ls
  2
  # cd 2
  # ls
  f7c2c5a71a29e73cf166505389455827
  # rm -rf *
http://s3.51cto.com/wyfs02/M00/75/9A/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_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.
  -------------------------------------------------------------------------------------
http://s3.51cto.com/wyfs02/M00/75/9A/wKiom1Y9uDuTNti0AAATTqEQP74758.jpg
  # vim /var/www/html/index.html
  nginx on node20 204.120
  # vim /var/www/html/index.html
  # service httpd restart
  停止 httpd:                                             [失败]
  正在启动 httpd:                                           [确定]
  # httpd -t
  Syntax OK
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim nginx.conf
  25      upstream upservers {       ##只能定义在http{..}
  26         server 192.168.204.140;
  27         server 192.168.204.120;
  28         }
  # 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      }
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  #service nginx>
  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M02/75/9B/wKiom1Y9vPySa3T0AAEr0pW7iII489.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # vim nginx.conf
  25      upstream upservers {
  26         server 192.168.204.140 weight=2;   ##加权轮询,默认=1;
  27         server 192.168.204.120;
  28         }

  # service nginx>  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M01/75/9B/wKiom1Y9vizx615qAAHG35GdlZ4935.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  ip_hash根据ip的hash调到单一的服务器
  # vim nginx.conf
  25      upstream upservers {
  26         ip_hash;         ## 默认:轮询
  27         server 192.168.204.140 weight=2;
  28         server 192.168.204.120;
  29         }
  # !ser

  service nginx>  重新载入 nginx:                                           [确定]
http://s3.51cto.com/wyfs02/M02/75/98/wKioL1Y9v4CjlG5UAAG9-7aNq44850.jpg
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  ip_hash根据ip的hash调到单一的服务器
  # 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         }
  # service nginx configtest
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  # service nginx>
  重新载入 nginx:                                           [确定]
  后端状态检测功能
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  #service httpd stop
  停止 httpd:                                             [确定]
  You have new mail in /var/spool/mail/root
http://s3.51cto.com/wyfs02/M01/75/98/wKioL1Y9wW_g5acMAAC22Qt6KzI341.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # service httpd start
  正在启动 httpd:                                           [确定]
http://s3.51cto.com/wyfs02/M00/75/9B/wKiom1Y9wjLwNBqKAAE4Mhd2jpU703.jpg
  -------------------------------------------------------------------------------------------
  通过backup停止访问,平滑升级,或者可以当备用
http://s3.51cto.com/wyfs02/M02/75/97/wKiom1Y9NY7QXoYrAAAUztyWkf0923.jpg
  # 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         }
  # !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

  # service nginx>  重新载入 nginx:          [确定]
http://s3.51cto.com/wyfs02/M00/75/98/wKioL1Y9xCfw0mx_AAC39kpn3uc355.jpg
http://s3.51cto.com/wyfs02/M00/75/95/wKioL1Y9NXWAVAi5AAAVtlUATnM584.jpg
  # service httpd stop
  停止 httpd:                                             [确定]
http://s3.51cto.com/wyfs02/M01/75/9B/wKiom1Y9xLyhCd3mAAC2aWkEanI247.jpg
  # service httpd start
  正在启动 httpd:                                           [确定]
http://s3.51cto.com/wyfs02/M01/75/98/wKioL1Y9xU6Qs0phAACknVdEgZU142.jpg


页: [1]
查看完整版本: nginx (1)