设为首页 收藏本站
查看: 627|回复: 0

[经验分享] Nginx技术指南

[复制链接]

尚未签到

发表于 2018-11-14 13:29:38 | 显示全部楼层 |阅读模式
./configure  make && make install
  cd ../[/code]
  代码:
以下内容为程序代码:  ./configure --user=www --group=www  --prefix=/usr/local/nginx/ --with-http_stub_status_module  --with-openssl=/usr/local/openssl
  make && make install
  更详细的模块定制与安装请参照官方wiki.
  三. Nginx Rewrite
  复制内容到剪贴板代码:last - 基本上都用这个Flag。
  break - 中止Rewirte,不在继续匹配
  redirect - 返回临时重定向的HTTP状态302
  permanent - 返回永久重定向的HTTP状态301
  其中:复制内容到剪贴板代码: * ~ 为区分大小写匹配
  * ~* 为不区分大小写匹配
  * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
  其中:复制内容到剪贴板代码: * -f和!-f用来判断是否存在文件
  * -d和!-d用来判断是否存在目录
  * -e和!-e用来判断是否存在文件或目录
  * -x和!-x用来判断文件是否可执行
  代码:
以下内容为程序代码:  $content_length
  $content_type
  $document_root
  $document_uri
  $host
  $http_user_agent
  $http_cookie
  $limit_rate
  $request_body_file
  $request_method
  $remote_addr
  $remote_port
  $remote_user
  $request_filename
  $request_uri
  $query_string
  $scheme
  $server_protocol
  $server_addr
  $server_name
  $server_port
  $uri
  四.Nginx Redirect
  将所有linuxtone.org与abc.linuxtone.org域名全部自跳转到http://www.linuxtone.org
  代码:
以下内容为程序代码:  server
  {
  listen 80;
  server_name linuxtone.org abc.linuxtone.org;
  index index.html index.php;
  root /data/www/wwwroot;
  if ($http_host !~ "^www\.linxtone\.org$") {
  rewrite ^(.*) http://www.linuxtone.org$1 redirect;
  }
  ........................
  }
  五.Nginx 目录自动加斜线:
  代码:
  
  1. if (-d $request_filename){
  2.   rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
  3.   }
复制代码
  六.Nginx 防盗链
  代码:
以下内容为程序代码:  #Preventing hot linking of images and other file types
  location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
  valid_referers none blocked server_names *.linuxtone.org http://localhost baidu.com;
  if ($invalid_referer) {
  rewrite ^/ http://www.linuxtone.org/images/default/logo.gif;
  # return 403;
  }
  }
  七.Nginx expires
  代码:
以下内容为程序代码:  # Add expires header for static content
  location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
  if (-f $request_filename) {
  root /data/www/wwwroot/bbs;
  expires 1d;
  break;
  }
  }
  代码:
以下内容为程序代码:  # serve static files
  location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  root /data/www/wwwroot/down;
  expires 30d;
  }
  八.Nginx 访问控制
  代码:
以下内容为程序代码:  #cd /usr/local/nginx/conf
  #mkdir htpasswd
  /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji linuxtone #添加用户名为linuxtone
  New password: (此处输入你的密码)
  Re-type new password: (再次输入你的密码)
  Adding password for user
  http://count.linuxtone.org/tongji/data/index.html(目录存在/data/www/wwwroot/tongji/data/目录下)
  将下段配置放到虚拟主机目录,当访问http://count.linuxtone/tongji/即提示要密验证:
  location ~ ^/(tongji)/ {
  root /data/www/wwwroot/count;
  auth_basic "LT-COUNT-TongJi";
  auth_basic_user_file /usr/local/nginx/conf/htpasswd/tongji;
  }
  如,Nginx下禁止访问*.txt文件,配置方法如下.
  代码:
以下内容为程序代码:  location ~* \.(txt|doc)$ {
  if (-f $request_filename) {
  root /data/www/wwwroot/linuxtone/test;
  break;
  }
  }
  方法2:
  代码:
以下内容为程序代码:  location ~* \.(txt|doc)${
  root /data/www/wwwroot/linuxtone/test;
  deny all;
  }
  禁止访问某个目录复制内容到剪贴板代码:location ~ ^/(WEB-INF)/ {
  deny all;
  }
代码:以下内容为程序代码:  location / {
  deny 192.168.1.1;
  allow 192.168.1.0/24;
  allow 10.1.1.0/16;
  deny all;
  }
  详细参见wiki: http://wiki.codemongers.com/NginxHttpAccessModule#allow
  代码:
以下内容为程序代码:  limit_zone one $binary_remote_addr 10m;
  server
  {
  listen 80;
  server_name down.linuxotne.org;
  index index.html index.htm index.php;
  root /data/www/wwwroot/down;
  #Zone limit
  location / {
  limit_conn one 1;
  limit_rate 20k;
  }
  ..........
  }
  代码:
以下内容为程序代码:  location / {
  autoindex on;
  }
  九.Nginx Location
  :[和上面rewrite正则匹配语法基本一致]
  代码:
  
  1. location [=|~|~*|^~] /uri/ { … }
  2.   * ~ 为区分大小写匹配
  3.   * ~* 为不区分大小写匹配
  4.   * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配
  5.   示例1:复制内容到剪贴板代码:location = / {
  6.   # matches the query / only.
  7.   # 只匹配 / 查询。
  8.   }
复制代码
  匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配
  示例2:
  代码:
以下内容为程序代码:  location ^~ /images/ {
  # matches any query beginning with /images/ and halts searching,
  # so regular expressions will not be checked.
  }
  # 匹配任何已 /images/ 开头的任何查询并且停止搜索。任何正则表达式将不会被测试。
  示例3:
  代码:
以下内容为程序代码:  location ~* \.(gif|jpg|jpeg)$ {
  # matches any request ending in gif, jpg, or jpeg. However, all
  # requests to the /images/ directory will be handled by
  }
  # 匹配任何已 gif、jpg 或 jpeg 结尾的请求。
  十.Nginx 日志处理
  代码:
  
  1. #contab -e
  2.   59 23 * * * /usr/local/sbin/logcron.sh /dev/null 2>&1
  3.   [root@count ~]# cat /usr/local/sbin/logcron.sh复制内容到剪贴板代码:#!/bin/bash
  4.   log_dir="/data/logs"
  5.   time=`date +%Y%m%d`
  6.   /bin/mv ${log_dir}/access_linuxtone.org.log ${log_dir}/access_count.linuxtone.org.$time.log
  7.   kill -USR1 `cat /var/run/nginx.pid`
复制代码
  更多的日志分析与处理就关注(同时欢迎你参加讨论):http://bbs.linuxtone.org/forum-8-1.html
  日志太多,每天好几个G,少记录一些,下面的配置写到server{}段中就可以了
  代码:
以下内容为程序代码:  location ~ .*\.(js|jpg|JPG|jpeg|JPEG|css|bmp|gif|GIF)$
  {
  access_log off;
  }
  十一.Nginx Cache服务配置
  如果需要将文件缓存到本地,则需要增加如下几个子参数:
  代码:
以下内容为程序代码:  proxy_store on;
  proxy_store_access user:rw group:rw all:rw;
  proxy_temp_path 缓存目录;
  其中,
  proxy_store on用来启用缓存到本地的功能,
  proxy_temp_path用来指定缓存在哪个目录下,如:proxy_temp_path html;
  在经过上一步配置之后,虽然文件被缓存到了本地磁盘上,但每次请求仍会向远端拉取文件,为了避免去远端拉取文件,必须修改proxy_pass:复制内容  到剪贴板代码:if ( !-e $request_filename) {
  proxy_pass http://mysvr;
  }
  即改成有条件地去执行proxy_pass,这个条件就是当请求的文件在本地的proxy_temp_path指定的目录下不存在时,再向后端拉取。
  十二.Nginx 负载均衡
  1. Nginx 基础知识
  nginx的upstream目前支持4种方式的分配
  1)、轮询(默认)
  每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
  2)、weight
  指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
  2)、ip_hash
  每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
  3)、fair(第三方)
  按后端服务器的响应时间来分配请求,响应时间短的优先分配。
  4)、url_hash(第三方)
  实例1
  代码
以下内容为程序代码:  :upstream bbs.linuxtone.org {#定义负载均衡设备的Ip及设备状态
  server 127.0.0.1:9090 down;
  server 127.0.0.1:8080 weight=2;
  server 127.0.0.1:6060;
  server 127.0.0.1:7070 backup;
  }
  在需要使用负载均衡的server中增加
  代码:
以下内容为程序代码:  proxy_pass http://bbs.linuxtone.org/;
  每个设备的状态设置为:
  代码:
以下内容为程序代码:  1.down 表示单前的server暂时不参与负载
  2.weight 默认为1.weight越大,负载的权重就越大。
  3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
  4.fail_timeout:max_fails次失败后,暂停的时间。
  5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
  nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
  client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
  client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
  location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡
  2
  按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效,也可以用作提高Squid缓存命中率.
  简单的负载均等实例:
  #vi nginx.conf //nginx主配置文件核心配置
  代码:
以下内容为程序代码:  ……….
  #loadblance my.linuxtone.org
  upstream my.linuxtone.org {
  ip_hash;
  server 127.0.0.1:8080;
  server 192.168.169.136:8080;
  server 219.101.75.138:8080;
  server 192.168.169.117;
  server 192.168.169.118;
  server 192.168.169.119;
  }
  …………..
  include vhosts/linuxtone_lb.conf;
  ………
  #vi proxy.conf
  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 50m;
  client_body_buffer_size 256k;
  proxy_connect_timeout 30;
  proxy_send_timeout 30;
  proxy_read_timeout 60;
  proxy_buffer_size 4k;
  proxy_buffers 4 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;
  proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
  proxy_max_temp_file_size 128m;
  proxy_store on;
  proxy_store_access user:rw group:rw all:r;
  #nginx cache
  client_body_temp_path /data/nginx_cache/client_body 1 2;
  proxy_temp_path /data/nginx_cache/proxy_temp 1 2;
  #vi linuxtone_lb.conf复制内容到剪贴板代码:server
  {
  listen 80;
  server_name my.linuxtone.org;
  index index.php;
  root /data/www/wwwroot/mylinuxtone;
  if (-f $request_filename) {
  break;
  }
  if (-f $request_filename/index.php) {
  rewrite (.*) $1/index.php break;
  }
  error_page 403 http://my.linuxtone.org/member.php?m=user&;a=login;
  location / {
  if ( !-e $request_filename) {
  proxy_pass http://my.linuxtone.org;
  break;
  }
  include /usr/local/nginx/conf/proxy.conf;
  }
  }
  十三.Nginx 优化
  默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以后一个nginx有好几兆。去掉nginx的debug模式编译,编译以后只有几百K
  在 auto/cc/gcc,最后几行有:
  # debug
  CFLAGS=”$CFLAGS -g”
  注释掉或删掉这几行,重新编译即可。
  代码:
  [code]# cd nginx-0.6.31
  # vi src/core/nginx.h
  #ifndef _NGINX_H_INCLUDED_
  #define _NGINX_H_INCLUDED_
  #define NGINX_VERSION "1.3"
  #define NGINX_VER "LTWS/" NGINX_VERSION
  #define NGINX_VAR "NGINX"
  #define NGX_OLDPID_EXT ".oldbin"
  #endif /* _NGINX_H_INCLUDED_ */
  # curl -I my.linuxtone.org
  HTTP/1.1 200 OK
  Server: LTWS/1.3
  Date: Mon, 24 Nov 2008 02:42:51 GMT
  Content-Type: text/html;
  Transfer-Encoding: chunked
  Connection: keep-alive


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635055-1-1.html 上篇帖子: TCMalloc优化Nginx性能 下篇帖子: 也谈谈Apache与Nginx
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表