• nginx配置详解-url重写、反向代理、负载均衡


    https://blog.csdn.net/jethai/article/details/52345302

    应用层的负载均衡

    master/worker结构:一个master进程,生成一个或多个worker子进程

    nginx请求的连接方式
    epoll 是Linux下多路复用IO接口select/poll的增强版本

    select 遍历
    epoll无需遍历

    1.nginx安装(未安装邮件服务器模块)

    ./configure --help查看编译选项

    配置文件中路径没加/以prefix指定的路径开始
    ./configure
    --prefix=/usr
    --sbin-path=/usr/sbin/nginx
    --conf-path=/etc/nginx/nginx.conf
    --error-log-path=/var/log/nginx/error.log
    --pid-path=/var/run/nginx/nginx.pid
    --lock-path=/var/lock/nginx.lock
    --user=nginx
    --group=nginx
    --with-http_ssl_module
    --with-http_flv_module
    --with-http_gzip_static_module
    --http-log-path=/var/log/nginx/access.log
    --http-client-body-temp-path=/var/tmp/nginx/client
    --http-proxy-temp-path=/var/tmp/nginx/proxy
    --http-fastcgi-temp-path=/var/tmp/nginx/fcgi
    --with-http_stub_status_module 

    make && make install

    2.nginx配置
    nginx -t -c  /etc/nginx/nginx.conf检测配置文件是否有语法错误


    ps aux|grep nginx


    #设置工作进程数,一般与cpu的核数一致
    work_processes 2;

    events {
    #单个进程最大连接数,超过就等待在队列排队
    worker_connections 1024;

    http{

    #日志格式定义
    log_format main '$remote_addr - $remote_user [$time_local]  "$request" '
                    '$status $body_bytes_sent "$http_refer" '
                    '"$http_user_agent" "$http_x_forward_for"';

    #Linux内存 操作系统和驱动程序运行在内核层,应用程序运行在用户层
    sendfile on;

    keepalive_timeout 65;


    #启用压缩功能
    gzip on;


    #反向代理缓存目录
    proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;

    #负载均衡
    upstream my_server_pool {
    server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
    server 192.168.1.10:80 weight=2 max_fails=2 fail_timeout=30;
    }

    #虚拟主机配置
    server {
    listen 192.168.1.1:80  default_server;
    server_name   www.example.org;
    root /var/www/web1;

    server{
    listen 80;
    server_name   www8.example.org;
    root /var/www/web2;

    #根据不同的浏览器URL重写
    if($http_user_agent ~ Firefox){
    rewrite ^(.*)$  /firefox/$1 break; 
    }
    if($http_user_agent ~ MSIE){
    rewrite ^(.*)$  /msie/$1 break; 
    }

    #实现域名跳转
    location / {
    rewrite ^/(.*)$ https://web8.example.com$1 permanent;
    }

    index index.html;
    #日志缓冲区
    access_log /var/log/nginx/www8.example.com-access.log main buffer=32k;
    error_log /var/log/nginx/www8.example.com-error.log warn;
    #什么样的日志文件描述符放到缓存中
    open_log_file_cache max=1000 inactive=20s min_uses=2 valid=1m;
    #防止盗链
    location ~* .(gif|jpg|png|swf|flv)$ {
    valid_referers none blocked www.test.com*.test.com;
    if($invalid_refer) {
    rewrite ^/(.*) http://www.test.com/block.html;
    }
    }

    #浏览器本地缓存设置
    #静态页面
    location ~ .*.(gif|jpg|jpge|png|bmp|swf|flv)$ {
    expires 30d;
    }
    #动态页面
    location ~ .*.(js|css)$ {
    expires 1h;
    }

    location /data {
    #自动索引开启,列出目录下的文件
    autoindex on;
    #将/data目录重写为/bbs
    rewrite ^/data/?$ /bbs permanent;
    #控制访问,相当于防火墙
    deny 192.168.0.132;
    allow 192.168.0.0/24;
    allow 192.168.1.1;
    deny all;
    #只允许.htpasswd文件中的用户访问
    #账号生成口令htpasswd -c /home/test1/a/.htpasswd username 
    #系统会要求输入两遍该用户的密码。
    #修改密码也是同样 htpasswd -c /home/test1/a/.htpasswd username
    auth_basic "AwstatAuth";
    auth_basic_user_file /etc/nginx/.htpasswd;
    }

    location /bbs {
    index index.html
    }
    location /b {
    #uri别名,路径过长简写
    alias /var/www/web2/data/redhat;
    }

    location /nginx_status {
    #nginx状态检查,用于监控nginx状态
    stub_status on;
    #不记录日志
    access_log off;
    }


    #自定义错误页面
    error_page 403 404  /40x.html;
    location /40x.html {
    root /var/www/error;
    }

    }

    #https访问 https://
    server {
    listen 443;
    server_name web8.example.com;

    ssl on;
    ssl_certificate  /etc/pki/tls/certs/httpd.crt;
    ssl_certificate_key /etc/pki/tls/private/httpd.key;

    ssl_session_timeout 5m;

    ssl_protocols SSLv2 SSLv3 TLSv1;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_cihers on;

    location / {
    root /var/www/web3;
    index index.html index.htm
    }

    #url重写和反向代理同时进行
    location /sports/ {
    proxy_pass http://192.168.0.2;
    }

    location /news/ {
    proxy_pass http://192.168.0.109:8080/bbs;
    proxy_cache_valid 200 10m;
    proxy_cache_valid 304 1m;
    proxy_cache_valid 301 302 1h;
    proxy_cache_valid any 1m;
    proxy_cache_key $host$uri$is_args$args;
    proxy_set_header Host $host; //后端日志记录的是远端地址而不是代理服务器本身
    proxy_cache cache_one;//引用cache
    proxy_set_header X-Forwarded-For $remote_addr;
    }

    #流媒体限速
    location /downlod {
    limit_rate_after 20m;//前20M不限速
    limit_rate 256K;
    }

    #反向代理加负载均衡
    location /sms {
    proxy_pass http://my_server_pool;
    }
    }


    nginx uri匹配规则(location)

    语法:location [=|~|~*|^~]  /uri/  {...}

    =:精确匹配
    ~:区分大小写
    ~*:不区分大小写
    ^~:禁止正则表达式匹配

    地址重写rewrite
    if指令 eturn 指令set rewrite 指令


    301 permanent 永久重定向,新网址完全继承旧网址,旧网址的排名等完全清零
    302 redirect 临时重定向 对旧网址没有影响,但新网址没有排名
    304 代表页面来自缓存

    rewrite 最后一项参数为flag标记
    1.last 浏览器URL地址不变
    2.break 浏览器URL地址不变
    3.redirect 浏览器会显示跳转后的url
    4.permanent 浏览器会显示跳转后的url

    last会对server标签重新发起一个新的请求,再次进入server块,重试location匹配

    break 直接使用当前location的资源来访问,不再执行location里余下的语句,完成本次请求

    一般在根location或server标签中推荐使用last标记,在非根location中,使用break 

    nginx日志管理(缓存)
    日志分割脚本(防止日志文件变得很庞大),每天分割一次
    vim /data/logs.sh
    #! /bin/bash
    #Nginx日志存放位置
    logs_path="/data/logs/"
    #将日志改名
    mkdir -p ${logs_path}${date -d "yesterday" +"%Y"}/${date -d "yesterday" +"%m"}/
    mv ${logs_path}access.log  ${logs_path}${date -d "yesterday" +"%Y"}/${date -d "yesterday" +"%m"}/access_${date -d "yesterday" +"%Y%m%d"}.log
    #重启Nginx服务,重新生成access.log文件
    service nginx reload


    #创建计划任务
    #crontab -|
    01 01 * * * /bin/bash /data/logs.sh

    负载均衡
    upstream my_server_pool {

    }

    把指定的输入文件拷贝到指定的输出文件中,并且在拷贝的过程中可以进行格式转换
    dd if=/dev/zero of=test bs=1M count=100

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/75/10/wKiom1YxtzWAUYLGAAX8kTBNTDU235.jpg" style="float:none;" title="誉天邹老师Nginx网站架构04-Nginx日志管理和限速.wmv_20151029_094023.869.jpg" alt="wKiom1YxtzWAUYLGAAX8kTBNTDU235.jpg" />

    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/75/0D/wKioL1Yxt2vjVK_kAASI482Fpcw903.jpg" style="float:none;" title="誉天邹老师Nginx网站架构02-Nginx虚拟主机及相关配置.wmv_20151028_214327.888.jpg" alt="wKioL1Yxt2vjVK_kAASI482Fpcw903.jpg" />
    ————————————————
    版权声明:本文为CSDN博主「小小程序员1986」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/jethai/article/details/52345302

  • 相关阅读:
    Python性能分析指南
    centos加入启动服务
    北京户口
    北京户口
    【汽车进口税】 关于汽车进口税计算你知道多少-汽车保险资讯-阳光车险官方网站
    在北京公司要多长时间才可以申请摇车牌号
    Kivy: Crossplatform Framework for NUI
    说说设计模式~大话目录(Design Pattern)
    说说设计模式~策略模式(Strategy)
    说说设计模式~装饰器模式(Decorator)
  • 原文地址:https://www.cnblogs.com/zhoading/p/12506801.html
Copyright © 2020-2023  润新知