nginx
转载请注明原文链接:http://blog.csdn.net/omohe/archive/2009/07/09/4335763.aspx 版本:v1.0 作者:OMO 最后修改时间:2009.07.09
0. nginx在实际中使用:
如果日后需要加入某个模块的话,需要重新编译,具体:
make clean,然后./configure+新的配置选项, make, make install。 难点是如何理解nginx服务器的配置,位于安装目录的nginx.conf文件。 总之需要知道的是:具体如何配置决定于准备使用nginx实现什么功能,例如最简单的单单是个反向代理服务器,或者作为一个http server来使用。各种具体应用和配置可以参看一下: "Typical Configurations Overview For Nginx HTTP(S) Reverse Proxy/Web Server": http://blog./2006/04/17/typical-nginx-configurations/ 更多详情参看关于如何配置的教程: http://wiki./NginxConfiguration 默认安装到/usr/local/nginx,具体启动的时候需要到bin目录下执行sudo ./nginx; 要终止nginx可以查看位于logs目录下的pid,然后kill pid即可; 2) 如果是二进制包安装的话: 首先搜搜看apt-cache search nginx 然后安装sudo apt-get install nginx Ubuntu下查看默认安装的位置,可以使用whereis nginx nginx和Apache类似都是通过module方式对各种功能进行扩展,关于nginx更多信息可以参看如上的链接。 3) Windows下的二进制安装非常简单: 直接下载,解压缩到C:/根目录下即可. 具体参看:http://wiki./NginxInstall nginx -s [ stop | quit | reopen | reload ] 3. nginx的配置: 上面介绍了,首先nginx和Apache类似通过各种模块module可以对服务器的功能进行丰富的扩展。同样也是类似通过nginx.conf文件可以对进行核心配置或者针对各种模块的配置。 http://wiki./NginxModules 具体如何配置通常还是取决于,准备使用nginx来做什么,几个很好的关于配置的参考资料: http://blog./2006/04/17/typical-nginx-configurations/ http://wiki./NginxConfiguration 0) 首先我们展示一下默认的nginx.conf文件中各个部分: nginx.conf配置文件中,针对不同的模块使用大括号包括起来,很方便地进行配置; 一个非常好的教程可以参看: http://wiki./NginxModules #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 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #这里表示设置该server的root根目录是nginx安装目录的html目录,当然可以设置绝对路径 location / { root html; autoindex on; #开启自动列出目录文件功能,如果找不到index页面的话。 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; } #这里是一个很好的nginx作为反向代理服务的设置,表示遇到php结尾的,会自动pass给后台侦听的apache或者别的服务器; #proxy the PHP scripts to Apache listening on 127.0.0.1:80 location ~ \.php$ { proxy_pass http://127.0.0.1 ; } #如果安装了FastCGI,则将其pass给FastCGI,这个说明了nginx本身不包裹FastCGI处理器,而是仅仅通过如下的方式pass给预先安装的FastCGI server,具体PHP的FastCGI如何安装,可以参看相关教程。 # 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; #} } #nginx的虚拟主机情况支持多种类型name-based(多个网站ip相同,域名不同),ip-based(多个网站ip不同) # 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; # } #} #提供SSL认证的server配置 # 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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 下面我们来简单介绍各种使用nginx的情况: 可以参考http://blog./2006/04/17/typical-nginx-configurations/ 1)默认安装的nginx配置文件中listen 80; 表示侦听80端口的HTTP请求,如果预先安装了Apache的话,就会占用该端口,从而使得Apache无法使用。 默认的配置提供对各种静态文件的访问; 2)nginx作为反向代理服务器使用 如上面的配置, #proxy the PHP scripts to Apache listening on 127.0.0.1:80 location ~ \.php$ { proxy_pass http://127.0.0.1 ; } 我们可以设置所有对php script脚本的请求,都proxy_pass到后台侦听的web server服务器(例如apache)等。 nginx还提供了一些模块例如Memcached,或者结合Squid等可以实现pass到后台web server相应之后,对返回的数据进行cache,从而提供性能。 这个相当于使用nginx作为apache的前端,可以看看这个教程: http:///posts/nginx-as-a-front-end-to-apache/ 常见的情况是,使用nginx直接处理静态的请求响应;对于动态的才pass给服务器,而且对服务器的响应结果进行缓存。 3)nginx做为多台server的负载均衡作用: http:///posts/load-balancing-with-nginx/ 4)另外一个常见的应用就是直接使用nginx代替了Apache,使用Linux+Nginx+MyQL+PHP的stack。 这个时候首先需要以FastCGI的方式安装PHP(需要一个FastCGI的process server),然后配置nginx支持PHP。 具体参考相关的专题介绍。 5)更多的配置介绍参看 http://wiki./NginxModules 各个模块的配置和configure教程。 |
|