nginx安装配置(及purge组件)

阅读:278 2019-03-19 14:41:43 来源:新网

一、nginx的安装与配置

1、从官网下载nginx及其组件purge(用于清除指定url缓存)

wgethttp://nginx.org/download/nginx-1.9.8.tar.gzwgethttp://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz

下载后分别进行解压:tar-zxvfnginx-1.9.8.tar.gz;tar-zxvfngx_cache_purge-2.3.tar.gz;

2、设置配置信息,在解压的nginx-1.9.8文件夹下执行:

./configure--user=www--group=www--prefix=/usr/local/nginx--with-pcre=/usr/local/temp/pcre-8.37--add-module=/usr/local/ngx_cache_purge

配置后执行(编译+安装)make&&makeinstall,过程中可能出现以下错误:(需要依赖其他安装包)

(1)执行后可能报错:checkingforos+linux2.6.32-358.el6.x86_64x86_64checkingforccompiler...notfound./configure:error:ccompilerccisnotfound

缺少gc,执行yuminstallgcc

(2)又报错了:./configure:error:thehttprewritemodulerequiresthepcrelibrary.youcaneitherdisablethemodulebyusing--without-http_rewrite_moduleoption,orinstallthepcrelibraryintothesystem,orbuildthepcrelibrarystaticallyfromthesourcewithnginxbyusing--with-pcre=option.是由于缺少pcrelibrary这个是httprewrite模块,也即是url静态化的包可上传pcre-8.37.tar.gz。从官网http://exim.mirror.fr/pcre/上下载pcre-8.37.tar.gz

#tar-zxvfpcre-8.37.tar.gz#cdpcre-8.37#./configure#make#makeinstall

(3)./configure:error:thehttpgzipmodulerequiresthezliblibrary.youcaneitherdisablethemodulebyusing–without-http_gzip_moduleoption,orinstallthezliblibraryintothesystem,orbuildthezliblibrarystaticallyfromthesourcewithnginxbyusing–with-zlib=option.则需要安装“zlib-devel”即可。ssh执行以下命令:

yuminstall-yzlib-devel

(4)在上述的#./configure时候有报错了:configure:error:youneedac++compilerforc++support.执行yuminstall-ygccgcc-c++

完成上述错误解决后,就可以继续:

3、nginx安装成功后的安装目录为/usr/local/nginx在conf文件夹中新建proxy.conf,用于配置一些代理参数,内容如下:

#!nginx(-)#proxy.confproxy_redirectoff;proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;#获取真实ip#proxy_set_headerx-forwarded-for$proxy_add_x_forwarded_for;#获取代理者的真实ipclient_max_body_size10m;client_body_buffer_size128k;proxy_connect_timeout90;proxy_send_timeout90;proxy_read_timeout90;proxy_buffer_size4k;proxy_buffers432k;proxy_busy_buffers_size64k;proxy_temp_file_write_size64k;

~~~~~~~~~~~~~~以下为配置文件内容解释~~~~~~~~~~~~~~~~~~~

nginx.conf中一些代理参数,内容如下:

#定义nginx运行的用户和用户组userwwwwww;

#nginx进程数,建议设置为等于cpu总核心数。worker_processes8;

#全局错误日志定义类型,[debug|info|notice|warn|error|crit]error_log/var/log/nginx/error.loginfo;

#进程文件pid/var/run/nginx.pid;

#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit-n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit-n的值保持一致。worker_rlimit_nofile65535;

#工作模式与连接数上限events{#参考事件模型,use[kqueue|rtsig|epoll|/dev/poll|select|poll];epoll模型是linux2.6以上版本内核中的高性能网络i/o模型,如果跑在freebsd上面,就用kqueue模型。useepoll;#单个进程最大连接数(最大连接数=连接数*进程数)worker_connections65535;}

#设定http服务器http{includemime.types;#文件扩展名与文件类型映射表default_typeapplication/octet-stream;#默认文件类型#charsetutf-8;#默认编码server_names_hash_bucket_size128;#服务器名字的hash表大小client_header_buffer_size32k;#上传文件大小限制large_client_header_buffers464k;#设定请求缓client_max_body_size8m;#设定请求缓sendfileon;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为on,如果用来进行下载等应用磁盘io重负载应用,可设置为off,以平衡磁盘与网络i/o处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。autoindexon;#开启目录列表访问,合适下载服务器,默认关闭。tcp_nopushon;#防止网络阻塞tcp_nodelayon;#防止网络阻塞keepalive_timeout120;#长连接超时时间,单位是秒

#fastcgi相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。fastcgi_connect_timeout300;fastcgi_send_timeout300;fastcgi_read_timeout300;fastcgi_buffer_size64k;fastcgi_buffers464k;fastcgi_busy_buffers_size128k;fastcgi_temp_file_write_size128k;

#gzip模块设置gzipon;#开启gzip压缩输出gzip_min_length1k;#最小压缩文件大小gzip_buffers416k;#压缩缓冲区gzip_http_version1.0;#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)gzip_comp_level2;#压缩等级gzip_typestext/plainapplication/x-javascripttext/cssapplication/xml;#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn。gzip_varyon;#limit_zonecrawler$binary_remote_addr10m;#开启限制ip连接数的时候需要使用

upstreamblog.ha97.com{#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。server192.168.80.121:80weight=3;server192.168.80.122:80weight=2;server192.168.80.123:80weight=3;}

#虚拟主机的配置server{#监听端口listen80;#域名可以有多个,用空格隔开server_namewww.ha97.comha97.com;indexindex.htmlindex.htmindex.php;root/data/www/ha97;location~.*.(php|php5)?${fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;includefastcgi.conf;}#图片缓存时间设置location~.*.(gif|jpg|jpeg|png|bmp|swf)${expires10d;}#js和css缓存时间设置location~.*.(js|css)?${expires1h;}#日志格式设定log_formataccess'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent"$http_x_forwarded_for';#定义本虚拟主机的访问日志access_log/var/log/nginx/ha97access.logaccess;

#对"/"启用反向代理location/{proxy_passhttp://127.0.0.1:88;proxy_redirectoff;proxy_set_headerx-real-ip$remote_addr;#后端的web服务器可以通过x-forwarded-for获取用户真实ipproxy_set_headerx-forwarded-for$proxy_add_x_forwarded_for;#以下是一些反向代理的配置,可选。proxy_set_headerhost$host;client_max_body_size10m;#允许客户端请求的最大单文件字节数client_body_buffer_size128k;#缓冲区代理缓冲用户端请求的最大字节数,proxy_connect_timeout90;#nginx跟后端服务器连接超时时间(代理连接超时)proxy_send_timeout90;#后端服务器数据回传时间(代理发送超时)proxy_read_timeout90;#连接成功后,后端服务器响应时间(代理接收超时)proxy_buffer_size4k;#设置代理服务器(nginx)保存用户头信息的缓冲区大小proxy_buffers432k;#proxy_buffers缓冲区,网页平均在32k以下的设置proxy_busy_buffers_size64k;#高负荷下缓冲大小(proxy_buffers*2)proxy_temp_file_write_size64k;#设定缓存文件夹大小,大于这个值,将从upstream服务器传}

#设定查看nginx状态的地址location/nginxstatus{stub_statuson;access_logon;auth_basic"nginxstatus";auth_basic_user_fileconf/htpasswd;#htpasswd文件的内容可以用apache提供的htpasswd工具来产生。}

#本地动静分离反向代理配置#所有jsp的页面均交由tomcat或resin处理location~.(jsp|jspx|do)?${proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;proxy_set_headerx-forwarded-for$proxy_add_x_forwarded_for;proxy_passhttp://127.0.0.1:8080;}#所有静态文件由nginx直接读取不经过tomcat或resinlocation~.*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)${expires15d;}location~.*.(js|css)?${expires1h;}}}~~~~~~~~~~~~~以上配置文件内容解释完结~~~~~~~~~~~~~~~~~~~~

4、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

#/usr/local/nginx/sbin/nginx-t如果屏幕显示以下两行信息,说明配置文件正确:

theconfigurationfile/usr/local/nginx/conf/nginx.confsyntaxisoktheconfigurationfile/usr/local/nginx/conf/nginx.confwastestedsuccessfully

如果提示unknownhost,则可在服务器上执行:pingwww.baidu.com如果也是同样提示unknownhost则有两种可能:a、服务器没有设置dns服务器地址,查看/etc/resolv.conf下是否设置,若无则加上b、防火墙拦截

5、启动nginx的命令

#/usr/local/nginx/sbin/nginx这时,输入以下命令查看nginx主进程号:ps-ef|grep"nginx:masterprocess"|grep-v"grep"|awk-f'''{print$2}'

6、停止nginx的命令

#/usr/local/nginx/sbin/nginx-sstop

【启动过程中出现的错误汇总】:

1、/usr/local/nginx/sbin/nginx-t校验conf配置时出错:

-bash:cd:/usr/local/nginx/sbin/nginx:notadirectory

检查缺少的命令:ldd$(which/usr/local/nginx/sbin/nginx)

结果:

linux-vdso.so.1=>(0x00007fffbc8a7000)libpthread.so.0=>/lib64/libpthread.so.0(0x0000003785c00000)libcrypt.so.1=>/lib64/libcrypt.so.1(0x0000003788400000)libpcre.so.1=>notfoundlibcrypto.so.10=>/usr/lib64/libcrypto.so.10(0x000000378a800000)libz.so.1=>/lib64/libz.so.1(0x0000003786800000)libc.so.6=>/lib64/libc.so.6(0x0000003785400000)/lib64/ld-linux-x86-64.so.2(0x0000003785000000)libfreebl3.so=>/lib64/libfreebl3.so(0x0000003788c00000)libdl.so.2=>/lib64/libdl.so.2(0x0000003785800000)解决:ln-s/usr/local/lib/libpcre.so.1/lib64

再次执行结果:

linux-vdso.so.1=>(0x00007fff899ff000)libpthread.so.0=>/lib64/libpthread.so.0(0x0000003785c00000)libcrypt.so.1=>/lib64/libcrypt.so.1(0x0000003788400000)libpcre.so.1=>/lib64/libpcre.so.1(0x00007f909decd000)libcrypto.so.10=>/usr/lib64/libcrypto.so.10(0x000000378a800000)libz.so.1=>/lib64/libz.so.1(0x0000003786800000)libc.so.6=>/lib64/libc.so.6(0x0000003785400000)/lib64/ld-linux-x86-64.so.2(0x0000003785000000)libfreebl3.so=>/lib64/libfreebl3.so(0x0000003788c00000)libdl.so.2=>/lib64/libdl.so.2(0x0000003785800000)

另外也会报错:errorwhileloadingsharedlibraries:libpcre.so.1:cannotopensharedobjectfile:nosuchfileordirectory,意思是找不到libpcre.so.1这个模块,而导致启动失败。

如果是32位系统ln-s/usr/local/lib/libpcre.so.1/lib如果是64位系统ln-s/usr/local/lib/libpcre.so.1/lib64然后在启动nginx就ok了/usr/local/webserver/nginx/sbin/nginx

2、[emerg]:bind()to0.0.0.0:80failed(98:addressalreadyinuse)

是nginx重复重启。自己占用了端口。解决方法killall-9nginx杀掉nginx进程然后重启就行了。servicenginxrestart

3、常用命令:

/usr/local/nginx/sbin/nginx-参数-c:使用指定的配置文件而不是conf目录下的nginx.conf。-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。-sreload重载-sstop停止

二、purge组件的使用:

在上述安装nginx过程中,第2步配置信息的时候已经配置了--add-module=/usr/local/ngx_cache_purge,则只需要在nginx/conf/nginx.conf配置。

~~~~~~~~~~~~~~以下为配置文件内容示例~~~~~~~~~~~~~~~~~userwwwwww;worker_processes24;

#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;

pidlogs/nginx.pid;

events{worker_connections1024;}

http{includemime.types;default_typeapplication/octet-stream;

log_formatmain'$remote_addr-$remote_user[$time_local]"$request"''$status$body_bytes_sent"$http_referer"''"$http_user_agent""$http_x_forwarded_for"$upstream_cache_status$request_time';proxy_cache_path/usr/local/nginx/cachekeys_zone=cache1:100minactive=1dmax_size=10g;access_loglogs/access.logmain;error_loglogs/error.logdebug;

sendfileon;#tcp_nopushon;

#keepalive_timeout0;keepalive_timeout65;client_max_body_size2m;

#gzipon;upstreamquroot{server127.0.0.1:8080;server127.0.0.1:8090;}upstreamquservice{#server127.0.0.1:8081;server139.196.198.62:8081;}

upstreamqucms{server127.0.0.1:8086;}

server{listen80;server_namewww.baidu.com;indexindex.jsp;

location~/purge(/.*){allowall;proxy_cache_purgecache1$host$1$is_args$args;}

location/quservice/{proxy_passhttp://quservice;proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;}

location/quapp/{proxy_passhttp://quroot;proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;}

location/qucms/{proxy_passhttp://qucms;proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;}

location~/(qu$){rewrite^/(.*)$http://www.baidu.com;}

location=/html/scamp/{root/usr/local/tomcat/static/html/scamp/join.html;}location~/(html|pic|video|video_minicar|tool|poi|apk)/{root/usr/local/tomcat/static;proxy_cachecache1;proxy_cache_key$host$uri$is_args$args;proxy_cache_valid20030430m;add_headerx-cache$upstream_cache_status;}

location/{add_headeraccess-control-allow-origin*;proxy_passhttp://quroot;proxy_set_headerhost$host;proxy_set_headerx-real-ip$remote_addr;}#error_page404/404.html;

#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{roothtml;}

}

}

相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服