码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx一点总结

时间:2017-03-16 22:40:22      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:负载均衡   web服务器   虚拟主机   代理服务器   

Nginx的总结

Nginx是常用的web服务的软件,比Apache性能和扩展性上要好,所以现在对Nginx的功能上做以下总结:

  Nginx的功能:

1、Web服务-à设置多虚拟主机的服务并配合fast-cgi或tomcat支持动态网页;

Nginx是近年来比较火的一个www服务的软件,与Apache和lighttpd以及tomcat等功能类似,但是nginx要比前者有着卓越的性能,比如:采用了epoll模型,内存消耗小等优点;

2、反向代理 -à多虚拟主机的代理;

指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服务器上得到的结果返回给Internet上请求连接的客户端;

3、七层的负载均衡—>单多虚拟主机不同服务器之间的访问;

负载均衡是由多台服务器以对称的方式组成一个服务器集合,每台都是等价地位,通过某种负载分担技术,将外部发送来的请求均匀分配到对称结构中某一台服务器上,来接收到请求的服务器独立地回应客户的请求;

4、正向代理 –>代理上网;

 代理内部网络对Internet的链接请求,客户机必须指定代理服务器,并将本来要直接发送到web服务器上的http请求发送到代理服务器中,由代理服务器请求并返回响应内容;

5、缓存服务

  为proxy和fastcgi做缓存服务,提高访问速度,相当于squid功能;

Nginx的优点:

1、高并发连接(采用epoll模型);2、内存消耗小;3、成本低(免费、开源);4、其他(配置简单、支持rewrite重写、内置健康检查、节省带宽gzip、稳定性高、支持热部署等)

Nginx的安装和配置:

1、查看系统版本和内核:

cat/etc/redhat-release

CentOS release6.7 (Final)

[root@LNMP-02 ~]#uname -r

2.6.32-573.el6.x86_64

2、安装nginx的依赖包:

rpm -aq pcrepcre-devel openssl openssl-devel

yum install pcrepcre-devel openssl openssl-devel -y

3、创建安装包下载路径(这个要养成习惯,也是作为好运维的标准)

mkdir –p  /server/xuesong/tools

cd  /server/xuesong/tools/

下载:wgethttp://nginx.org/download/nginx-1.6.3.tar.gz

ls nginx-1.6.3.tar.gz -----下载成功

nginx-1.6.3.tar.gz

4、创建nginx用户(虚拟用户)并解压并编译nginx:

useradd nginx -s/sbin/nologin -M

tar xfnginx-1.6.3.tar.gz

cd nginx-1.6.3

./configure--user=nginx --group=nginx --prefix=/app/nginx-1.6.3--with-http_stub_status_module --with-http_ssl_module

echo $?  0                   ------看是否有错误

mkdir /app

make

make install

ln -s/app/nginx-1.6.3/ /app/nginx

ll /app/nginx

做软连接并检查语法及启动nginx服务:

lrwxrwxrwx. 1root root 17 May 26 06:56 /app/nginx -> /app/nginx-1.6.3/

[root@LNMP-02nginx-1.6.3]# /app/nginx/sbin/nginx -t

nginx: theconfiguration file /app/nginx-1.6.3/conf/nginx.conf syntax is ok

nginx:configuration file /app/nginx-1.6.3/conf/nginx.conf test is successful

[root@LNMP-02nginx-1.6.3]# /app/nginx/sbin/nginx

[root@LNMP-02nginx-1.6.3]# lsof -i :80

COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   7511 root    6u  IPv4  24823      0t0 TCP *:http (LISTEN)

nginx   7512 nginx   6u  IPv4  24823     0t0  TCP *:http (LISTEN)

服务启动完毕,用window浏览器访问IP测试:

更改主页并测试:

cd /app/nginx

vim./html/index.html

并在此用windows测试。

Nginx主要组成文件:

tree ./

|--client_body_temp

|-- conf

|   |-- fastcgi.conf          ---------->这是fastcgi的主配置文件

|   |-- fastcgi.conf.default

|   |-- fastcgi_params

|   |-- fastcgi_params.default

|   |-- koi-utf

|   |-- koi-win

|   |-- mime.types         ---------->这是媒体类型

|   |-- mime.types.default

|   |-- nginx.conf             ---------->这是nginx的主配置文件

|   |-- nginx.conf.default

|   |-- scgi_params

|   |-- scgi_params.default

|   |-- uwsgi_params

|   |-- uwsgi_params.default

|   `-- win-utf

|-- fastcgi_temp

|-- html                               ---------->这是nginx的默认站点目录

|   |-- 50x.html

|   `-- index.html

|-- logs                           ---------->这是nginx的错误和访问日志

|   |-- access.log

|   |-- error.log

|   `-- nginx.pid

|-- proxy_temp

|-- sbin                          ---------->这是nginx的命令目录,如启动命令

|   `-- nginx

|-- scgi_temp

`-- uwsgi_temp

9 directories, 21files

配置和优化的事项:

一、配置文件的归类(xx.conf和fast-cgi.conf等)

基础的主配置文件nginx.conf只有一个,如果配置web或proxy服务时,所有的配置和参数都配置到nginx.conf中看起来比较混乱而且不易于管理,所以把虚拟主机的配置分拆xx.conf和fast-cgi.conf等,放置到conf/下,并在主配置文件Nginx.conf中添加include,使之生效并加载到配置文件中;

二、虚拟主机程序的归类(www或bbs等)

   基础(默认)的虚拟主机的首页文件在/html/下,但是如果你有多个虚拟主机可以在html/下建立多个文件夹比如www或bbs等,并在对应的配置文件中更改root 标签项,对应的路径:

location / {

         root  html/www;

         index index.html index.htm;

        }

三、Nginx的主配置文件的示例(说明):

组成结构:

技术分享

-------------------------------------------------------------------------------

…………

events

{

……..

}

http

{

………….

Server

{

……

}

Server

{

……

}

…………

}

-------------------------------------------------------------------------------

配置说明:

#user:指定 Nginx Worker进程运行用户和用户组,默认 nobody 账号
user nginx;
groupnginx;
     #worker_processes:
指定 Nginx 要开启的进程数,建议和 cpu 数量一样的
worker_processes 1;
     # error_log:定义全局错误日志文件。日志有输出级别:
[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;
         #进程文件
pid /var/run/nginx.pid;
  #一个 nginx 进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值 ulimit-n)与 nginx 进程数相除,但是 nginx 分配请求并不均匀,所以建议与 ulimit -n 的值保持一致。
worker_rlimit_nofile 65535;
#工作模式与连接数上限
events
{

 #参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll |select | poll ]; epoll 模型是 Linux 2.6 以上版本内核中的高性能网络 I/O 模型,如果跑在 FreeBSD 上面,就用 kqueue模型。
use epoll;
    #单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 65535;
}

 #设定服务器
http
{
include mime.types;   
 #文件拓展名和文件类型映射表
default_type application/octet-stream;   #默认文件类型
charset utf-8;
#默认编码
server_names_hash_bucket_size 128;
#服务器名字的 bash 表大小
client_header_buffer_size 32k;
#上传文件大小限制
large_client_header_buffers 4 64k;
client_max_body_size 8m;
sendfile on;
#开启高效文件传输模式
autoindex on;#开启目录表访问,合适下载服务器,默认关闭
tcp_nopush on;
防止网络阻塞
tcp_nodelay on;
#防止网络阻塞
keepalive_timeout 120;
#长连接超时时间,单位是秒
    #FastCGI 相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。

fastcgi_connect_timeout300; 132
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#gzip 模块设置
gzip on; #
开启 gzip 压缩文件大小
gzip_min_length 1k; #最小压缩文件大小
gzip_buffers 4 16k; #压缩缓冲区
gzip_http_version 1.1;#压缩版本
gzip_comp_level 2; #压缩等级
gzip_types text/plain application/x-javascript text/css application/xml;
  
#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个 warn。
gzip_vary on;
  #limit_zone crawler $binary_remote_addr 10m; #开启限制 IP 连接数的时候需要使用
upstream zhouxuesong.com{
 
 #upstream 的负载均衡,weight 是权重,可以根据机器配置定义权重。weigth 参数表示权值,权值越高被分配到的几率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#日志格式设定
log_formataccess ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" $http_x_forwarded_for‘;

#虚拟主机的配置
server
{

#监听端口
listen 80;
#域名可以有多个,用空格隔开
server_name www.zhouxuesong.com xuesong.com
index index.html index.htm index.php;
root /data/www/
location ~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}

#图片缓存时间设置
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$

{
expires 10d;
}
#JS 和 CSS 缓存时间设置
location ~ .*.(js|css)?$
{
expires 1h;
}

#定义本虚拟主机的访问日志
access_log  /var/log/nginx/access.log access;
#对 "/" 启用反向代理
location / {
proxy_pass http://zhouxuesong.com;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
 #后端的 Web 服务器可以通过 X-Forwarded-For 获取用户真实 IP
proxy_set_header  X-Forwarded-For $remote_addr;
 #以下是一些反向代理的配置,可选。
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 服务器传
}

#设定查看 Nginx 状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd
文件的内容可以用 apache 提供的 htpasswd 工具来产生。

}
#本地动静分离反向代理配置
#所有 jsp 的页面均交由 tomcat 或 resin 处理

location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1:8080;
}

#所有静态文件由 nginx 直接读取不经过 tomcat 或 resin
location~.*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
} }

nginx.conf里面的参数都做了注释或详解,对大的区块做一个小结:

1、       虚拟主机(域名、端口、IP地址)

2、       Log日志(存放位置、格式)

3、       Nginx的压缩输出配置

4、       Nginx的浏览器本地缓存设置

5、       与php以及tomcat动态网页配合

6、       Nginx的反向代理和负载均衡

不同的区域对应的功能(核心功能模块):

1、       nginx作为web服务的配置示例:

http{

    include       mime.types;

 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"‘;

   sendfile       on;

cp_nopush     on;

   keepalive_timeout  65;

gzip  on;

    server {

        listen       80;

        server_name  www.zhouxuesong.com;

       access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

注意说明:

如果web服务器作为代理服务器后端的real server那么log的格式就是以上所示,但是还需要在proxy server配置文件内(proxy_pass下方)加上proxy_set_headerX-Forwarded-For $remote_addr; proxy_set_header Host $host;这两个参数,后端服务才能生效!(格式在http标签里面、access_log在server标签里面)

nginx与动态网页(PHP、java)配合配置示例:

location~ .*.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;       -
à配置文件在conf/下,这是针对fastcgi优化配置
}

 java(Tomcat)

location~ .(jsp|jspx|do)?$ {
proxy_set_headerHost $host;
proxy_set_header
X-Forwarded-For$remote_addr;
proxy_pass http://127.0.0.1:8080;
}

nginx浏览器本地缓存设置

location~ .*.(gif|jpg|jpeg|png|bmp|swf)$

{
expires 10d;
    }
#JS
和 CSS 缓存时间设置
location ~ .*.(js|css)?$
  {
expires 1h;
   }

Nginx代理服务器(反向代理和负载均衡)

upstreamzhouxuesong.com {
server 192.168.80.121:80 weight=3 max_fail=3 fail_timeout=30s;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}

#在sever{}标签里面,一个虚拟主机;

location/ {
proxy_pass http://zhouxuesong.com;
proxy_redirect off;
  #
后端的 Web 服务器可以通过 X-Forwarded-For 获取用户真实 IP

proxy_set_header X-Forwarded-For $remote_addr;
  #
以下是一些反向代理的配置,可选。
proxy_set_header Host $host;

nginx的正向代理:

     正向代理就是通常所说的内网通过服务器来上Internet网的这种方式,这台服务器就叫代理服务器;

……

Server

{

listen8080;

location/ {

           resolver 8.8.8.8;

            proxy_passhttp://$host$request_uri;

}

  access_log /logs/proxy.log;

}

………….

以上是proxy的代理服务器上的配置,最后在客户端的IE上设置LAN代理填写。

nginx的缓存服务器(pass_cachefastcgi_cache):

proxy_cache和fastcgi_cache构成了Nginx的缓存,proxy_cache主要用于反向代理时,对后端内容源服务器进行缓存,可能是任何内容,包括静态的和动态,缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带;fastcgi_cache主要用于对FastCGI的动态程序进行缓存,很多情况是php生成的动态的内容,fastcgi_cache缓存减少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力,这比用memcached之类的缓存要轻松得多。。两者的功能基本上一样。

pass_cache (代理缓存):

mkdir–p /data0/proxy_temp_path     注:两个缓存文件必须要在同一磁盘分区,不能

mkdir–p /data0/proxy_cache_path         夸分区。

Nginx配置文件nginx.conf:对扩展名为gif\jpg\jpeg、 swf\bmp、的图片、flash、js\css、文件开启web缓存,其他文件不缓存;

proxy_temp_path  /data0/ proxy_temp_path

proxy_cache_path  /data0/proxy_cache_path levels=1:2keys_zone=cache_one:200m inactive=1d max_size=30g;

注:上面这些参数都是在http{}标签里面;

location~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

{
proxy_cache cache_one  ;  #
使用web缓存区cache_one

proxy_cache_valid200 304 12h;

proxy_cache_valid301 302 1m;

proxy_cache_validany 1m;

#对不同http状态码缓存设置不同的缓存时间

proxy_cache_key$host$uri$is_args$args;

proxy_passhttp://www.zhouxuesong.com;

proxy_set_headerHost $host;

proxy_set_headerX-Forwarded-For $remote_addr;
    }

Fastcgi_cache(fastcgi缓存):

mkdir–p /data0/fastcgi_temp_path    注:两个缓存文件必须要在同一磁盘分区,不能

mkdir–p /data0/fastcgi_cache_path         夸分区。

Nginx配置文件nginx.conf:对扩展名为php文件开启web缓存,其他文件不缓存;

fastcgi_temp_path  /data0/ fastcgi _temp_path

fastcgi_cache_path /data0/fastcgi_cache_pathlevels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

注:这些参数都是在http{}标签里面;

location~ .*.(php|php5)?$
{
fastcgi _cache cache_one    #
使用web缓存区cache_one

fastcgi_cache_valid 200 304 12h;

fastcgi_cache_valid 301 302 1m;

fastcgi_cache_valid any 1m;

#对不同http状态码缓存设置不同的缓存时间

fastcgi_cache_key $host$uri$is_args$args;
    }

nginx日志的轮询 nginx rwrite 规则

Nginx的日志切割和轮询:

#mv www.access.log www.access_$(date +%F -d -1day).log

#/application/nginx/sbin/nginx -s reload

 


本文出自 “雪松” 博客,请务必保留此出处http://zhouxuesong.blog.51cto.com/1457230/1907224

Nginx一点总结

标签:负载均衡   web服务器   虚拟主机   代理服务器   

原文地址:http://zhouxuesong.blog.51cto.com/1457230/1907224

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!