基础篇

  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 匹配特定位置的设置),部分内容源自网上。
  1. user www www;#nginx用户及组:用户 组。window下不指定
  2. worker_processes 4;#一般设置为 CPU数*核数
  3. error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;#管理nginx进程
  7. events {
  8. use epoll;#使用epollI/O 模型。linux建议epollFreeBSD建议采用kqueuewindow下不指定
  9. worker_connections 51024;#指一个子进程最大允许连51024个连接
  10. }
  11. http {
  12. include mime.types;#设定mime类型
  13. default_type application/octet-stream;
  14. #设置日志类型
  15. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  16. # '$status $body_bytes_sent "$http_referer" '
  17. # '"$http_user_agent" "$http_x_forwarded_for"';
  18. #access_log logs/access.log main;
  19. #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用
  20. sendfile on;
  21. #tcp_nopush on;
  22. #连接超时时间
  23. #keepalive_timeout 0;
  24. keepalive_timeout 65;
  25. #是否开启gzip压缩
  26. #gzip on;
  27. server {
  28. listen 80;
  29. server_name localhost;
  30. #charset koi8-r;
  31. #日志类型
  32. #access_log logs/host.access.log main;
  33. #默认请求
  34. location / {
  35. root html;
  36. index index.html index.htm;
  37. }
  38. #error_page 404 /404.html;
  39. # redirect server error pages to the static page /50x.html
  40. #
  41. # 定义错误提示页面
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. root html;
  45. }
  46. #静态文件,nginx自己处理
  47. location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  48. root /var/www/html;
  49. #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
  50. expires 30d;
  51. }
  52. #设定负载均衡的服务器列表
  53. upstream mysvr {
  54. #weigth参数表示权值,权值越高被分配到的几率越大
  55. #本机上的Squid开启3128端口
  56. server 192.168.8.1:3128 weight=5;
  57. server 192.168.8.2:80 weight=1;
  58. server 192.168.8.3:80 weight=6;
  59. }
  60. #对 "/" 启用反向代理
  61. location / {
  62. proxy_pass http://127.0.0.1:88;
  63. proxy_redirect off;
  64. proxy_set_header X-Real-IP $remote_addr;
  65. #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
  66. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  67. #以下是一些反向代理的配置,可选。
  68. proxy_set_header Host $host;
  69. client_max_body_size 10m; #允许客户端请求的最大单文件字节数
  70. client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数,
  71. proxy_connect_timeout 90; #nginx跟后端服务器连接超时时间(代理连接超时)
  72. proxy_send_timeout 90; #后端服务器数据回传时间(代理发送超时)
  73. proxy_read_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时)
  74. proxy_buffer_size 4k; #设置代理服务器(nginx)保存用户头信息的缓冲区大小
  75. proxy_buffers 4 32k; #proxy_buffers缓冲区,网页平均在32k以下的设置
  76. proxy_busy_buffers_size 64k; #高负荷下缓冲大小(proxy_buffers*2)
  77. proxy_temp_file_write_size 64k;
  78. #设定缓存文件夹大小,大于这个值,将从upstream服务器传
  79. }
  80. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  81. #
  82. #location ~ \.php$ {
  83. # proxy_pass http://127.0.0.1;
  84. #}
  85. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  86. #
  87. #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
  88. location ~ \.php$ {
  89. root html;
  90. fastcgi_pass 127.0.0.1:9000;
  91. fastcgi_index index.php;
  92. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  93. include fastcgi.conf;
  94. }
  95. # deny access to .htaccess files, if Apache's document root
  96. # concurs with nginx's one
  97. #
  98. #location ~ /\.ht {
  99. # deny all;
  100. #}
  101. }
  102. # another virtual host using mix of IP-, name-, and port-based configuration
  103. #
  104. #server {
  105. # listen 8000;
  106. # listen somename:8080;
  107. # server_name somename alias another.alias;
  108. # location / {
  109. # root html;
  110. # index index.html index.htm;
  111. # }
  112. #}
  113. # HTTPS server
  114. #
  115. #server {
  116. # listen 443 ssl;
  117. # server_name localhost;
  118. # ssl_certificate cert.pem;
  119. # ssl_certificate_key cert.key;
  120. # ssl_session_cache shared:SSL:1m;
  121. # ssl_session_timeout 5m;
  122. # ssl_ciphers HIGH:!aNULL:!MD5;
  123. # ssl_prefer_server_ciphers on;
  124. # location / {
  125. # root html;
  126. # index index.html index.htm;
  127. # }
  128. #}
  129. }
  1. Nginx配置虚拟主机

    这个在之前博文中已经介绍到,跟Apache配置虚拟主机类似,有基于ip、基于端口和基于域名三种方式设置虚拟主机。博文链接:http://www.cnblogs.com/weblm/p/5537749.html
  2. Nginx日志管理

    在Nginx的配置文件中server段的访问日志信息access_log logs/host.access.log main;类型,在http段有这种类型的定义

  1. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  2. # '$status $body_bytes_sent "$http_referer" '
  3. # '"$http_user_agent" "$http_x_forwarded_for"';
  1. 一般情况下,我们需要对日志进行切割,会用到这些日志信息。具体的操作主要是shell脚本+定时任务,在此略过

实际运用篇

  1. 配置nginx+php

    编译php+nginx之前已经介绍过,nginx+php的配置简单总结:把请求的信息转发给9000端口的PHP进程, 让PHP进程处理 指定目录下的PHP文件。与Apache相比,apache一般是把php当做自己的一个模块来启动的,而nginx则是把http请求变量(如get,user_agent等)转发给 php进程,即php独立进程,与nginx进行通信. 称为 fastcgi运行方式。
  1. location ~ \.php$ {
  2. root html;
  3. fastcgi_pass 127.0.0.1:9000;
  4. fastcgi_index index.php;
  5. `# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  6. include fastcgi.conf;
  7. }
  1. Nginx与Rewrite规则

    Rewrite语法:Rewrite 正则表达式 定向后的位置 模式

    默认值:none

    使用字段:server, location, if

    典型的可以实现301重定向
  1. server {
  2. listen 80;
  3. server_name www.testhost.com;
  4. #charset koi8-r;
  5. access_log logs/testhost.access.log main;
  6. rewrite ^/(.*)$ http://www.test.com/$1 permanent;
  7. }

Nginx反向代理和负载均衡

  1. 反向代理

    nginx用proxy_pass实现做反向代理功能,可以实现多域名的跳转、反向代理缓存等。

    多域名跳转:

  1. server www.test.com
  2. location / {
  3. proxy_pass http://192.168.115.131/web/
  4. }
  5. location /admin {
  6. proxy_pass http://192.168.115.131/admin
  7. }
  8. server m.test.com
  9. location / {
  10. proxy_pass http://192.168.115.131/wap/
  11. }
  1. 举个栗子,nginx处理静态文件的能力特别强,可以将图片、cssjs等请求转发给nginx来处理。

  1. location ~ \.(js|css|flash|jpg|jpeg|png|gif)$ {
  2. proxy_set_header X-Forwarded-For $remote_addr;
  3. proxy_pass IP:port;
  4. }
  1. 负载均衡

    负载均衡是大型网站中常用的一种解决方案,有硬件负载均衡(F5 BIG-IP ,硬件负载均衡(很贵).直接从TCP/IP的底层协议上,直接做数据包的中转),软件负载均衡(LVS、Nginx)。Nginx负载均衡实现算法主要有DNS轮询、Weight、ip_hash。默认是轮询方式,可以安装第三方模块利用不同参数把请求均衡到不同服务器去。

  1. http {
  2. upstream myserver {
  3. server 192.168.115.132:80 weight=3 max_fails=3 fail_timeout=20s;
  4. server 192.168.115.132:80 weight=1 max_fails=3 fail_timeout=20s;
  5. server 192.168.115.132:80 weight=4 max_fails=3 fail_timeout=20s;
  6. }
  7. server {
  8. listen 80;
  9. server_name www.domain.com 192.168.115.132;
  10. index index.htm index.html;
  11. root /data/web/wwwroot;
  12. location / {
  13. proxy_pass http://myserver;
  14. proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
  15. include /app/local/nginx/conf/proxy.conf;
  16. }
  17. }
  18. }

Nginx笔记的更多相关文章

  1. CentOS 6.4 快速安装Nginx笔记

    CentOS 6.4 快速安装Nginx笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. http://blog.csdn.net/ex_net/article/details/9860 ...

  2. nginx笔记5-双机热备原理

    1动静分离演示: 将笔记3的Demo改造一下,如图所示: 改造完成后,其实就是在网页上显示一张图片 现在启动Tomcat运行起来,如图: 可以看到图片的请求是请求Tomcat下的图片. 现在,通过把静 ...

  3. nginx笔记4-负载均衡带来的问题以及解决办法

    接着笔记3,将笔记三的改造一下,现在分别启动两个Tomcat,在页面获取session.如图所示: tomcat2的session: tomcat1的session: 根据上图发现,每个tomcat取 ...

  4. nginx笔记3-负载均衡算法

    1.nginx测试:先从官网下载nginx 官网网址为:http://nginx.org/  然后找到stable version的版本下载,因为这版本是最稳定的,不要去下载最新,因为不稳定,如下图: ...

  5. nginx笔记2-负载均衡

    负载均衡实现方式分为两类:1硬件类,2软件类. 硬件类:F5(这是一种硬件,并不是刷新啊,不要误解)  优点:非常快,可靠性高,并发量大.缺点:太贵,成本高,不方便,最致命的是不能将动态请求和静态请求 ...

  6. Nginx笔记02-nginx常用参数配置说明

    nginx的主配置文件是nginx.conf,这里主要针对这个文件进行说明 1.主配置文件nginx.conf   2.nginx配置文件的结构 从上面的配置文件中我们可以总结出nginx配置文件的基 ...

  7. 同事不太懂负载均衡,我直接把阿里架构师的这份Nginx笔记甩给他

    Nginx功能强大,架构复杂,学习.维护和开发的门槛较高. 本份笔记深入最新的Nginx源码,详细剖析了模块体系.动态插件.功能框架.进程模型.事件驱动.线程池.TCP/UDP/HTTP 处理等Ngi ...

  8. 【网络】安装Nginx笔记

    目录 前言 安装前先更新下 安装依赖库 下载Nginx Nginx编译配置 编译&安装&验证nginx Nginx服务配置 配置SSL 参考 前言 up安装nginx主要是为了在服务器 ...

  9. [Nginx笔记]关于线上环境CLOSE_WAIT和TIME_WAIT过高

    运维的同学和Team里面的一个同学分别遇到过Nginx在线上环境使用中会遇到TIME_WAIT过高或者CLOSE_WAIT过高的状态 先从原因分析一下为什么,问题就迎刃而解了. 首先是TIME_WAI ...

随机推荐

  1. 【代码笔记】iOS-字符串的分割

    一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. ...

  2. Git 少用 Pull 多用 Fetch 和 Merge

    本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样:我没有时间让它更短些.在Git的邮件列表里有很多关于本文的讨论,我会尽量把其中相关的观点列在下面. 我最常 ...

  3. freeswitch注册过程分析

    操作系统:debian8.5_x64freeswitch 版本 : 1.6.8 本文仅描述sip注册的简单场景,即话机直接向处于同一个局域网的fs进行注册. SIP协议的消息结构 消息框架 SIP协议 ...

  4. Ctrl-A全选这点事(C#,WinForm)

    所有的文本框,不管单行多行都Ctrl-A全选就好了吧?是啊,很方便.Windows的软件基本都是这样.可为什么我们自己制作的WinForm就默认不是这样呢?谁知道呢,可能是WinForm饱受诟病,要改 ...

  5. 关于Oracle表连接

    表连接注意left join on与where的区别: select * from dept; select * from emp; select * from emp a right outer j ...

  6. mongodb简介(转)

    1.简介 MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数 ...

  7. JAVA设计模式之3-抽象工厂模式

    书接上文,简单工厂模式解决的是可以枚举种类的类的问题,但是带来了高耦合的问题,并且对类系列繁多无从下手,那么我们想起了一种方法,那就是抽象类,建一个抽象工厂,抽象工厂里的方法都是根据系列类的差异区分出 ...

  8. CWMP开源代码研究5——CWMP程序设计思想

    声明:本文涉及的开源程序代码学习和研究,严禁用于商业目的. 如有任何问题,欢迎和我交流.(企鹅号:408797506) 本文介绍自己用过的ACS,其中包括开源版(提供下载包)和商业版(仅提供安装包下载 ...

  9. 微信H5中的一些坑

    最近在写微信公众号H5页面 遇到了一些坑,在这里记录一下 记录一下signature的计算 // 首先找到hex_sha1的加密算法,ticket 是后端提供的 var url_local = loc ...

  10. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...