• Nginx笔记


    基础篇

    1. 关于Nginx
      Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。最早由俄罗斯的程序设计师Igor Sysoev所开发,并在一个BSD-like 协议下发行。其特点是轻量级,占有内存少,并发能力强。目前在国内很多大型互联网企业得到广泛应用。
    2. 安装Nginx
      linux下安装nginx在前面的博文中已经介绍到,在此不在赘述。windows下直接在官网下载windows下Stable版解压即可,需要对nginx.conf做相应配置,跟linux下类似。
    3. 常用命令参数
      可以通过信号控制nginx的启动、关闭、重载以及检测配置文件是否正确等,也可以通过nginx程序自带的一些命令参数控制。常见如下:
      nginx -t 测试配置是否正确
      nginx -s reload , 作用:加载最新配置
      nginx -s reopen 作用: 重新打开日志
      nginx -s stop 作用: 立即停止
      nginx -s quit 作用: 优雅停止
      使用信号控制:kill -信号选项 nginx主进程号,信号选项有HUP(改变配置文件,平滑的重读配置文件)、USR1(重读日志,在日志按月/日分割时有用)、USR2(平滑的升级)、WINCH(优雅关闭旧的进程(配合USR2来进行升级))等,可以通过ps -ef|grep nginx查看nginx主进程号。

    应用篇

    1. Nginx配置详解
      nginx 配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(负载均衡服务器设置)、location(URL 匹配特定位置的设置),部分内容源自网上。
    
    
    
        user www www;#nginx用户及组:用户 组。window下不指定
        worker_processes  4;#一般设置为 CPU数*核数
    
        error_log  logs/error.log;
        #error_log  logs/error.log  notice;
        #error_log  logs/error.log  info;
    
        #pid        logs/nginx.pid;#管理nginx进程
    
    
        events {
            use epoll;#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定
            worker_connections  51024;#指一个子进程最大允许连51024个连接
        }
    
    
        http {
            include       mime.types;#设定mime类型
            default_type  application/octet-stream;
            #设置日志类型
            #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
            #                  '$status $body_bytes_sent "$http_referer" '
            #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
            #access_log  logs/access.log  main;
    
            #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用
            sendfile        on;
            #tcp_nopush     on;
    
            #连接超时时间
            #keepalive_timeout  0;
            keepalive_timeout  65;
        
            #是否开启gzip压缩
            #gzip  on;
    
            server {
                listen       80;
                server_name  localhost;
    
                #charset koi8-r;
                #日志类型
                #access_log  logs/host.access.log  main;
            
                #默认请求
                location / {
                    root   html;
                    index  index.html index.htm;
                }
    
                #error_page  404              /404.html;
    
                # redirect server error pages to the static page /50x.html
                # 
                # 定义错误提示页面
                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                    root   html;
                }
                #静态文件,nginx自己处理
                location ~ ^/(images|javascript|js|css|flash|media|static)/ {
                    root /var/www/html;
                    #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
                    expires 30d;
                }
                #设定负载均衡的服务器列表
                 upstream mysvr {
                    #weigth参数表示权值,权值越高被分配到的几率越大
                    #本机上的Squid开启3128端口
                    server 192.168.8.1:3128 weight=5;
                    server 192.168.8.2:80  weight=1;
                    server 192.168.8.3:80  weight=6;
                }
                #对 "/" 启用反向代理
                location / {
                    proxy_pass http://127.0.0.1:88;
                    proxy_redirect off;
                    proxy_set_header X-Real-IP $remote_addr;
                    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    #以下是一些反向代理的配置,可选。
                    proxy_set_header Host $host;
                    client_max_body_size 10m; #允许客户端请求的最大单文件字节数
                    client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
                    proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
                    proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
                    proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
                    proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
                    proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
                    proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
                    proxy_temp_file_write_size 64k;
                    #设定缓存文件夹大小,大于这个值,将从upstream服务器传
                }
                # proxy the PHP scripts to Apache listening on 127.0.0.1:80
                #
                #location ~ .php$ {
                #    proxy_pass   http://127.0.0.1;
                #}
    
                # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
                #
                #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
                location ~ .php$ {
                   root           html;
                   fastcgi_pass   127.0.0.1:9000;
                   fastcgi_index  index.php;
                  # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                   include        fastcgi.conf;
                }
    
                 # deny access to .htaccess files, if Apache's document root
                # concurs with nginx's one
                #
                #location ~ /.ht {
                #    deny  all;
                #}
    
            }
    
    
            # another virtual host using mix of IP-, name-, and port-based configuration
            #
            #server {
            #    listen       8000;
            #    listen       somename:8080;
            #    server_name  somename  alias  another.alias;
    
            #    location / {
            #        root   html;
            #        index  index.html index.htm;
            #    }
            #}
    
    
            # HTTPS server
            #
            #server {
            #    listen       443 ssl;
            #    server_name  localhost;
    
            #    ssl_certificate      cert.pem;
            #    ssl_certificate_key  cert.key;
    
            #    ssl_session_cache    shared:SSL:1m;
            #    ssl_session_timeout  5m;
    
            #    ssl_ciphers  HIGH:!aNULL:!MD5;
            #    ssl_prefer_server_ciphers  on;
    
            #    location / {
            #        root   html;
            #        index  index.html index.htm;
            #    }
            #}
    
        }
    
    1. Nginx配置虚拟主机
      这个在之前博文中已经介绍到,跟Apache配置虚拟主机类似,有基于ip、基于端口和基于域名三种方式设置虚拟主机。博文链接:http://www.cnblogs.com/weblm/p/5537749.html
    2. Nginx日志管理
      在Nginx的配置文件中server段的访问日志信息access_log logs/host.access.log main;类型,在http段有这种类型的定义
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    一般情况下,我们需要对日志进行切割,会用到这些日志信息。具体的操作主要是shell脚本+定时任务,在此略过
    

    实际运用篇

    1. 配置nginx+php
      编译php+nginx之前已经介绍过,nginx+php的配置简单总结:把请求的信息转发给9000端口的PHP进程, 让PHP进程处理 指定目录下的PHP文件。与Apache相比,apache一般是把php当做自己的一个模块来启动的,而nginx则是把http请求变量(如get,user_agent等)转发给 php进程,即php独立进程,与nginx进行通信. 称为 fastcgi运行方式。
         location ~ .php$ {
                   root           html;
                   fastcgi_pass   127.0.0.1:9000;
                   fastcgi_index  index.php;
                  `# fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                   include        fastcgi.conf;
                }
    
    1. Nginx与Rewrite规则
      Rewrite语法:Rewrite 正则表达式 定向后的位置 模式
      默认值:none
      使用字段:server, location, if
      典型的可以实现301重定向
        server {
            listen 80;
            server_name www.testhost.com;
            #charset koi8-r;
            access_log logs/testhost.access.log main;
            rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    
        }
    

    Nginx反向代理和负载均衡

    1. 反向代理
      nginx用proxy_pass实现做反向代理功能,可以实现多域名的跳转、反向代理缓存等。
      多域名跳转:
    
        server www.test.com
        location / {
            proxy_pass http://192.168.115.131/web/
        }
        location /admin {
            proxy_pass http://192.168.115.131/admin
        }
        server m.test.com
        location / {
            proxy_pass http://192.168.115.131/wap/
        }
    
    举个栗子,nginx处理静态文件的能力特别强,可以将图片、css、js等请求转发给nginx来处理。
    
    
        location ~ .(js|css|flash|jpg|jpeg|png|gif)$ {
                proxy_set_header X-Forwarded-For $remote_addr;
    	    proxy_pass IP:port;
        }
    
    1. 负载均衡
      负载均衡是大型网站中常用的一种解决方案,有硬件负载均衡(F5 BIG-IP ,硬件负载均衡(很贵).直接从TCP/IP的底层协议上,直接做数据包的中转),软件负载均衡(LVS、Nginx)。Nginx负载均衡实现算法主要有DNS轮询、Weight、ip_hash。默认是轮询方式,可以安装第三方模块利用不同参数把请求均衡到不同服务器去。
    
        http {
                upstream myserver {
                server 192.168.115.132:80 weight=3 max_fails=3 fail_timeout=20s;
                server 192.168.115.132:80 weight=1 max_fails=3 fail_timeout=20s;
                server 192.168.115.132:80 weight=4 max_fails=3 fail_timeout=20s;
         }
        server {
            listen 80;
            server_name www.domain.com 192.168.115.132;
            index index.htm index.html;
            root /data/web/wwwroot;
                location / {
                    proxy_pass http://myserver;
                    proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
                    include /app/local/nginx/conf/proxy.conf;
    
                }
              }
        }
    
  • 相关阅读:
    Beta 冲刺day 6
    Beta冲刺day5
    Beta冲刺day4
    Beta 冲刺day3
    Beta 冲刺day2
    Beta冲刺day1
    Beta预备
    城市安全风险管理项目Postmortem结果
    项目总结
    Alpha冲刺置顶随笔
  • 原文地址:https://www.cnblogs.com/weblm/p/5905599.html
Copyright © 2020-2023  润新知