加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

Nginx 安装与详解

发布时间:2020-12-13 21:42:30 所属栏目:Nginx 来源:网络整理
导读:nginx简介 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件。它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用。nginx比它大哥apache性能改进许多,nginx占用的系统资源更少,支持更高的并发连接,有更高的访问效率。ngin

nginx简介

nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件。它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用。nginx比它大哥apache性能改进许多,nginx占用的系统资源更少,支持更高的并发连接,有更高的访问效率。nginx不但是一个优秀的web服务软件,还可以作为反向代理,负载均衡,以及缓存服务使用。安装更为简单,方便,灵活。

优点

  支持高并发,能支持几万并发连接   资源消耗少,在3万并发连接下开启10个nginx线程消耗的内存不到200M   可以做http反向代理和负载均衡   支持异步网络i/o事件模型epoll

安装

1,下载Nginx相关配置(此步很重要)

yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

2,下载和解压

wget -c https://nginx.org/download/nginx-1.12<span style="color: #008000;">#<span style="color: #008000;"> 2,在当前目录下解压
tar -zxvf nginx-1.12.0.tar.gz

3,配置和编译安装

cd nginx-1.12<span style="color: #008000;">#<span style="color: #008000;"> 2,指定安装位置
./configure --prefix=/opt/nginx112/

<span style="color: #008000;">#<span style="color: #008000;"> 3,编译且安装
make && make install

4,启动nginx

cd /opt/<span style="color: #008000;">#<span style="color: #008000;"> 2,进入软件的具体目录
<span style="color: #000000;">cd sbin

<span style="color: #008000;">#<span style="color: #008000;"> 相关命令
./nginx <span style="color: #008000;">#<span style="color: #008000;">启动
./nginx -s stop <span style="color: #008000;">#<span style="color: #008000;">关闭
./nginx -s reload <span style="color: #008000;">#<span style="color: #008000;">平滑重启 ,修改了nginx.conf之后,可以不重启服务,加载新的配置

nignx详情解析

1,安装完成后检测服务

netstat -tunlp |grep 80-I 127.0.0.1

2,部署一个web站点

nginx默认站点是Nginx目录下的html文件夹,这里可以从nginx.conf中查到

location / index index.html index.htm; }

如果要部署网站业务数据,只需要把开发好的程序全放到html目录下即可

[root@python /tmp 11:34:52] index.html jssts.jpeg lhy.mp4 man.jpg wget-log

因此只需要通过域名/资源,即可访问

192.168.11.31/man.jpg

3,nginx的目录结构

[root@python /opt/nginx112 11:44:02] client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp static uwsgi_temp
  • conf 存放nginx所有配置文件的目录,主要nginx.conf
  • html 存放nginx默认站点的目录,如index.html、error.html等
  • logs 存放nginx默认日志的目录,如error.log access.log
  • sbin 存放nginx主命令的目录,sbin/nginx

4,nginx主配置文件解析

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

<span style="color: #008000;">#<span style="color: #008000;">定义Nginx运行的用户和用户组
<span style="color: #000000;">user www www;

<span style="color: #008000;">#<span style="color: #008000;">nginx进程数,建议设置为等于CPU总核心数。
worker_processes 8<span style="color: #000000;">;

<span style="color: #008000;">#<span style="color: #008000;">全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
error_log /usr/local/nginx/logs/<span style="color: #000000;">error.log info;

<span style="color: #008000;">#<span style="color: #008000;">进程pid文件
pid /usr/local/nginx/logs/<span style="color: #000000;">nginx.pid;

<span style="color: #008000;">#<span style="color: #008000;">指定进程可以打开的最大描述符:数目<span style="color: #008000;">

<span style="color: #008000;">工作模式与连接数上限<span style="color: #008000;">

<span style="color: #008000;">这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。<span style="color: #008000;">

<span style="color: #008000;">现在在linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。<span style="color: #008000;">

<span style="color: #008000;">这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。

worker_rlimit_nofile 65535<span style="color: #000000;">;

events
{
<span style="color: #008000;">#<span style="color: #008000;">参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型
<span style="color: #008000;">#<span style="color: #008000;">是Linux 2.6以上版本内核中的高性能网络I/O模型,linux建议epoll,如果跑在FreeBSD上面,就用kqueue模型。
<span style="color: #008000;">#<span style="color: #008000;">补充说明:
<span style="color: #008000;">#<span style="color: #008000;">与apache相类,nginx针对不同的操作系统,有不同的事件模型
<span style="color: #008000;">#<span style="color: #008000;">A)标准事件模型
<span style="color: #008000;">#<span style="color: #008000;">Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
<span style="color: #008000;">#<span style="color: #008000;">B)高效事件模型
<span style="color: #008000;">#<span style="color: #008000;">Kqueue:使用于FreeBSD 4.1+,OpenBSD 2.9+,NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
<span style="color: #008000;">#<span style="color: #008000;">Epoll:使用于Linux内核2.6版本及以后的系统。
<span style="color: #008000;">#<span style="color: #008000;">/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
<span style="color: #008000;">#<span style="color: #008000;">Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。
<span style="color: #000000;"> use epoll;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;单个进程最大连接数(最大连接数=连接数*进程数)</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。</span>
worker_connections 65535<span style="color: #000000;"&gt;;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;keepalive超时时间。</span>
keepalive_timeout 60<span style="color: #000000;"&gt;;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;分页大小可以用命令getconf PAGESIZE 取得。</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;[root@web001 ~]# getconf PAGESIZE</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;4096</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。</span>

<span style="color: #000000;"> client_header_buffer_size 4k;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。</span>
open_file_cache max=65535 inactive=<span style="color: #000000;"&gt;60s;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;这个是指多长时间检查一次缓存的有效信息。</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http,server,location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.</span>

<span style="color: #000000;"> open_file_cache_valid 80s;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http,location  这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.</span>
open_file_cache_min_uses 1<span style="color: #000000;"&gt;;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http,location 这个指令指定是否在搜索一个文件是记录cache错误.</span>

<span style="color: #000000;"> open_file_cache_errors on;
}

<span style="color: #008000;">#<span style="color: #008000;">设定http服务器,利用它的反向代理功能提供负载均衡支持
<span style="color: #000000;">http
{
<span style="color: #008000;">#<span style="color: #008000;">文件扩展名与文件类型映射表
<span style="color: #000000;"> include mime.types;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;默认文件类型</span>
default_type application/octet-<span style="color: #000000;"&gt;stream;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;默认编码</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;charset utf-8;</span>

<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;服务器名字的hash表大小</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.</span>
server_names_hash_bucket_size 128<span style="color: #000000;"&gt;;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。</span>

<span style="color: #000000;"> client_header_buffer_size 32k;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果header过大,它会使用large_client_header_buffers来读取。</span>
large_client_header_buffers 4<span style="color: #000000;"&gt; 64k;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设定通过nginx上传文件的大小</span>

<span style="color: #000000;"> client_max_body_size 8m;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。</span>

<span style="color: #000000;"> sendfile on;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;开启目录列表访问,合适下载服务器,默认关闭。</span>

<span style="color: #000000;"> autoindex on;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用</span>

<span style="color: #000000;"> tcp_nopush on;

tcp_nodelay on;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;长连接超时时间,单位是秒</span>
keepalive_timeout 120<span style="color: #000000;"&gt;;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。</span>
fastcgi_connect_timeout 300<span style="color: #000000;"&gt;;
fastcgi_send_timeout </span>300<span style="color: #000000;"&gt;;
fastcgi_read_timeout </span>300<span style="color: #000000;"&gt;;
fastcgi_buffer_size 64k;
fastcgi_buffers </span>4<span style="color: #000000;"&gt; 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;gzip模块设置</span>
gzip on; <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;开启gzip压缩输出</span>
gzip_min_length 1k;    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;最小压缩文件大小</span>
gzip_buffers 4 16k;    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;压缩缓冲区</span>
gzip_http_version 1.0;    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;压缩版本(默认1.1,前端如果是squid2.5请使用1.0)</span>
gzip_comp_level 2;    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;压缩等级</span>
gzip_types text/plain application/x-javascript text/css application/xml;    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;压缩类型,默认就已经包含textml,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。</span>

<span style="color: #000000;"> gzip_vary on;

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;开启限制IP连接数的时候需要使用</span>
<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;limit_zone crawler $binary_remote_addr 10m;</span>



<span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;负载均衡配置</span>

<span style="color: #000000;"> upstream jh.52php.cn {

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。</span>
    server 192.168.80.121:80 weight=3<span style="color: #000000;"&gt;;
    server </span>192.168.80.122:80 weight=2<span style="color: #000000;"&gt;;
    server </span>192.168.80.123:80 weight=3<span style="color: #000000;"&gt;;

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;nginx的upstream目前支持4种方式的分配</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;1、轮询(默认)</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;2、weight</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;例如:</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream bakend {</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 192.168.0.14 weight=10;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 192.168.0.15 weight=10;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;2、ip_hash</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;例如:</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream bakend {</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    ip_hash;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 192.168.0.14:88;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 192.168.0.15:80;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;3、fair(第三方)</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;按后端服务器的响应时间来分配请求,响应时间短的优先分配。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream backend {</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server server1;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server server2;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    fair;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;4、url_hash(第三方)</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream backend {</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server squid1:3128;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server squid2:3128;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    hash $request_uri;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    hash_method crc32;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</span>

    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;tips:</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;upstream bakend{#定义负载均衡设备的Ip及设备状态}{</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    ip_hash;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 127.0.0.1:9090 down;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 127.0.0.1:8080 weight=2;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 127.0.0.1:6060;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;    server 127.0.0.1:7070 backup;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;}</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;在需要使用负载均衡的server中增加 proxy_pass http://bakend/;</span>

    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;每个设备的状态设置为:</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;1.down表示单前的server暂时不参与负载</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;2.weight为weight越大,负载的权重就越大。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;4.fail_timeout:max_fails次失败后,暂停的时间。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。</span>

    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;nginx支持同时设置多组的负载均衡,用来给不用的server来使用。</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;client_body_temp_path设置记录文件的目录 可以设置最多3层目录</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡</span>

<span style="color: #000000;"> }

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;虚拟主机的配置</span>

<span style="color: #000000;"> server
{
<span style="color: #008000;">#<span style="color: #008000;">监听端口
listen 80<span style="color: #000000;">;

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;域名可以有多个,用空格隔开</span>

<span style="color: #000000;"> server_name www.52php.cn 52php.cn;
index index.html index.htm index.php;
root /data/www/<span style="color: #000000;">w3cschool;

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;对******进行负载均衡</span>
    location ~ .*.(php|<span style="color: #000000;"&gt;php5)?$
    {
        fastcgi_pass </span>127.0.0.1:9000<span style="color: #000000;"&gt;;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;图片缓存时间设置</span>
    location ~ .*.(gif|jpg|jpeg|png|bmp|<span style="color: #000000;"&gt;swf)$
    {
        expires 10d;
    }

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;JS和CSS缓存时间设置</span>
    location ~ .*.(js|<span style="color: #000000;"&gt;css)?$
    {
        expires 1h;
    }

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;日志格式设定</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$remote_user:用来记录客户端用户名称;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$time_local: 用来记录访问时间与时区;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$request: 用来记录请求的url与http协议;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$status: 用来记录请求状态;成功是200,</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$body_bytes_sent :记录发送给客户端文件主体内容大小;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$http_referer:用来记录从那个页面链接访问过来的;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;$http_user_agent:记录客户浏览器的相关信息;</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。</span>
    log_format access <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;$remote_addr - $remote_user [$time_local] "$request" </span><span style="color: #800000;"&gt;'</span>
    <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;$status $body_bytes_sent "$http_referer" </span><span style="color: #800000;"&gt;'</span>
    <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;"$http_user_agent" $http_x_forwarded_for</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;;

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;定义本虚拟主机的访问日志</span>
    access_log  /usr/local/nginx/logs/<span style="color: #000000;"&gt;host.access.log  main;
    access_log  </span>/usr/local/nginx/logs/host.access.404<span style="color: #000000;"&gt;.log  log404;

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;对 "/" 启用反向代理</span>
    location /<span style="color: #000000;"&gt; {
        proxy_pass http:</span>//127.0.0.1:88<span style="color: #000000;"&gt;;
        proxy_redirect off;
        proxy_set_header X</span>-Real-<span style="color: #000000;"&gt;IP $remote_addr;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span>
        proxy_set_header X-Forwarded-<span style="color: #000000;"&gt;For $proxy_add_x_forwarded_for;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;以下是一些反向代理的配置,可选。</span>

<span style="color: #000000;"> proxy_set_header Host $host;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;允许客户端请求的最大单文件字节数</span>

<span style="color: #000000;"> client_max_body_size 10m;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;缓冲区代理缓冲用户端请求的最大字节数,</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误</span>

<span style="color: #000000;"> client_body_buffer_size 128k;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;表示使nginx阻止HTTP应答代码为400或者更高的应答。</span>

<span style="color: #000000;"> proxy_intercept_errors on;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;后端服务器连接的超时时间_发起握手等候响应超时时间</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;nginx跟后端服务器连接超时时间(代理连接超时)</span>
        proxy_connect_timeout 90<span style="color: #000000;"&gt;;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;后端服务器数据回传时间(代理发送超时)</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据</span>
        proxy_send_timeout 90<span style="color: #000000;"&gt;;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;连接成功后,后端服务器响应时间(代理接收超时)</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)</span>
        proxy_read_timeout 90<span style="color: #000000;"&gt;;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设置代理服务器(nginx)保存用户头信息的缓冲区大小</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小</span>

<span style="color: #000000;"> proxy_buffer_size 4k;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;proxy_buffers缓冲区,网页平均在32k以下的设置</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k</span>
        proxy_buffers 4<span style="color: #000000;"&gt; 32k;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;高负荷下缓冲大小(proxy_buffers*2)</span>

<span style="color: #000000;"> proxy_busy_buffers_size 64k;

        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长</span>
        <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设定缓存文件夹大小,大于这个值,将从upstream服务器传</span>

<span style="color: #000000;"> proxy_temp_file_write_size 64k;
}

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;设定查看Nginx状态的地址</span>
    location /<span style="color: #000000;"&gt;NginxStatus {
        stub_status on;
        access_log on;
        auth_basic </span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;NginxStatus</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;;
        auth_basic_user_file confpasswd;
        </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;htpasswd文件的内容可以用apache提供的htpasswd工具来产生。</span>

<span style="color: #000000;"> }

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;本地动静分离反向代理配置</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;所有jsp的页面均交由tomcat或resin处理</span>
    location ~ .(jsp|jspx|<span style="color: #000000;"&gt;do)?$ {
        proxy_set_header Host $host;
        proxy_set_header X</span>-Real-<span style="color: #000000;"&gt;IP $remote_addr;
        proxy_set_header X</span>-Forwarded-<span style="color: #000000;"&gt;For $proxy_add_x_forwarded_for;
        proxy_pass http:</span>//127.0.0.1:8080<span style="color: #000000;"&gt;;
    }

    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt;所有静态文件由nginx直接读取不经过tomcat或resin</span>
    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|<span style="color: #000000;"&gt;
    pdf</span>|xls|mp3|<span style="color: #000000;"&gt;wma)$
    {
        expires 15d; 
    }

    location </span>~ .*.(js|<span style="color: #000000;"&gt;css)?$
    {
        expires 1h;
    }
}

}
<span style="color: #008000;">#<span style="color: #008000;">#####Nginx配置文件nginx.conf中文详解#####
<span style="color: #000000;">
nginx.conf详解

user www; <span style="color: #008000;">#<span style="color: #008000;">Nginx进程所使用的用户
worker_processes 1; <span style="color: #008000;">#<span style="color: #008000;">Nginx运行的work进程数量(建议与CPU数量一致或auto)
error_log /log/nginx/error.log <span style="color: #008000;">#<span style="color: #008000;">Nginx错误日志存放路径
pid /var/run/nginx.pid <span style="color: #008000;">#<span style="color: #008000;">Nginx服务运行后产生的pid进程号

events {
worker_connections
<span style="color: #008000;">#<span style="color: #008000;"> 每个worker进程支持的最大连接数
use epool; <span style="color: #008000;">#<span style="color: #008000;"> 事件驱动模型,epoll默认
}

http { 'server'80; server_name localhost; access_log host.access.log 'location' //usr/share/nginx/html; index index.html index.htm; error_page 500 502 503 504 /50x.html; 'server'include </span>/etc/nginx/conf.d/*.conf; <span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt;包含/etc/nginx/conf.d/目录下所有以.conf结尾的文件</span>

<span style="color: #000000;">
} <span style="color: #008000;">//<span style="color: #008000;">http层结束

附加惊喜

附上一款黑客帝国般的linx屏保安装

wget https://jaist.dl.sourceforge.net/project/cmatrix/cmatrix/1.2a/cmatrix-1<span style="color: #008000;">#<span style="color: #008000;"> 2.解压缩源码包
tar -zxvf cmatrix-1<span style="color: #000000;">.2a.tar.gz

<span style="color: #008000;">#<span style="color: #008000;"> 3.进入源码包目录
cd cmatrix-1.2a/

<span style="color: #008000;">#<span style="color: #008000;"> 4.释放编译文件
./configure --prefix=/opt/cmatrix/

<span style="color: #008000;">#<span style="color: #008000;"> 5.编译且安装
make &&<span style="color: #000000;"> make install

<span style="color: #008000;">#<span style="color: #008000;"> 6.进入安装屏保软件的目录
cd /opt/cmatrix/

<span style="color: #008000;">#<span style="color: #008000;"> 7.执行屏保命令
./bin/cmatrix

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读