nginx的配置及使用
Nginx ("engine x") 是一个高性能的 HTTP和反向代理服务器,据说他可以同时处理10万的并发访问量,这个我就没法确认了。
Nginx官方地址是:http://nginx.org
上面提供了windows和unix版本的nginx,有稳定版也有开发板,可以根据具体情况选择。
现在开始我们的nginx之旅吧!
首先,从nginx下载了windows版本的压缩包,解压后看到
这样的目录结构,熟悉apache的朋友应该很快就明白各个目录的作用。比较重要的就是conf和html这两个文件夹,html是默认放置网站内容的文件夹,conf是配置文件放置的地方,打开conf发现
但是貌似只有fastcgi.conf和nginx.conf可以编辑。
打开nginx.conf文件:
#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}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"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #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; } # 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 # #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_params; #} # 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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
?初学者看见这些可能已经头疼了,熟悉apache配置的朋友估计笑了。
我们就找几个重要的说说吧(当然想要让配置生效要先去掉前面的#号):
#linux/unix下那个用户运行nginxuser nobody; #进程运行数量,根据cpu数量或其他需求配置worker_processes 1; #错误日志error_log logs/error.log;error_log logs/error.log notice;error_log logs/error.log info;#nginx的pid配置 pid百度百科:http://baike.baidu.com/view/402830.htm#2pid logs/nginx.pid;#每个进程的最大连接数量,最大连接数量=worker_processes*worker_connectionsevents { worker_connections 1024;}http {#mime类型配置,mime百度百科:http://baike.baidu.com/view/160611.htm 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"'; access_log logs/access.log main;#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush配置,tcp_nopush和tcp_nodelay开启可以提高nginx性能,具体作用:http://www.oschina.net/question/17_2516 tcp_nopush on;#连接活动时间,即超时时间 keepalive_timeout 65;#压缩传输,gzip百度百科:http://baike.baidu.com/view/966625.htm gzip on;#服务配置 server { #监听端口,即什么端口可以访问到nginx,与tomcat,apache一样 listen 80; #服务器名称 server_name localhost;#文字编码 charset koi8-r; access_log logs/host.access.log main; location / { #默认网页放置位置,就是我们上面说到的html文件夹 root html; #主页,类似于<welcome-file-list> index index.html index.htm; }#错误页面 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # 设置php代理,即php处理服务器,比如127.0.0.1:80是apache服务器,就交由apache处理php的请求 location ~ \.php$ { #代理服务器地址,eg:http://127.0.0.1:8080,端口为80,"80"可省略 proxy_pass http://127.0.0.1; } #php FastCGI服务器的一些配置 location ~ \.php$ { root html; #定义解析php程序使用的FastCGI接口 fastcgi_pass 127.0.0.1:9000; #php首页 fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } } # 第一台虚拟主机,当需要用到多台虚拟住址的时候,只有复制server{}中的内容就可以了 server { listen 8000; listen somename:8080; server_name somename alias another.alias; location / { root html; index index.html index.htm; } } # HTTPS 服务器 server { listen 443; server_name localhost; ssl on; ssl_certificate cert.pem; ssl_certificate_key cert.key; ssl_session_timeout 5m; ssl_protocols SSLv2 SSLv3 TLSv1; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { root html; index index.html index.htm; } }}
?在windows下启动nginx非常简单,运行nginx.exe就可以了,打开测试地址:http://127.0.0.1看看是否显示了nginx的欢迎页,显示说明已经成功启动。
下面说下linux下安装nginx:
linux下安装nginx需要PCRE的支持,PCRE:http://www.pcre.org/,按照网站上的指导安装即可。
安装之前可以先删除已经安装的低版本的pcre:
#查找已安装的pcrerpm -qa|grep pcre#显示结果为:pcre-6.6-2.el5_1.7#在删除系统自带的PCRE之前,要先备份一下libpcre.so.0这个文件,rpm有关联性,当强制删除pcre时libpcre.so.0也会被删除,这样就会导致新的pcre安装不上,不信你可以试试cp /lib/libpcre.so.0 /home/g21121/#删除pcrerpm -e --nodeps pcre-6.6-2.el5_1.7
?
然后安装新的pcre:
#赋予执行权限chmod 755 pcre-8.30#解压pcretar -zxvf pcre-8.30 #进入pcre目录cd pcre-8.30#拷贝libpcre.so.0到libcp /home/g21121/libpcre.so.0 /lib/#采用默认路径就可以,避免没必要的麻烦./configure#编译make#安装make install
???安装pcre 注意执行顺序,先拷贝再删除,再拷贝再安装,这样就成功了。
?? 安装完成后,我们开始安装nginx:
./configure --without-http_rewrite_module --without-http_gzip_modulemakemake install
?安装完成,如果安装过程中遇到错误,可以根据提示增加configure后面的参数即可,下面是configure的参数列表:
?