nginx的简介,安装和简单配置

[size=large]nginx的简介,安装和简单配置


转载请注明原文链接:http://blog.csdn.net/omohe/archive/2009/07/09/4335763.aspx

0. nginx在实际中使用:
之前我们介绍了使用nginx可以完全作为web app server来使用,代替Apache;
另外也可以单单使用nginx作为反向代理服务器来实现Server Cluster。


1. nginx的基础知识:
参考http://en.wikipedia.org/wiki/Nginx
http://wiki.nginx.org/Main
http://www.nginx.org/
简单来说,精华在于:HTTPServer或者reverse proxy
另 外需要注意它和ApacheServer的不同,Unlike traditional servers, Nginx doesn't rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but most importantly, predictable amounts of memory under load.
Nginx scales in all directions: from the smallest VPS all the way up to clusters of servers.
另外,更多的功能可以通过模块进行扩展。

2. nginx的安装:
具体可以参考:http://wiki.nginx.org/Main( 支持Windows和Linux等多种OS)
1) 源代码安装很简单:可以按照默认的./configure,然后make和make install
输入./configure --help可以配置需要编译的各种模块,

如果日后需要加入某个模块的话,需要重新编译,具体:
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.kovyrin.net/2006/04/17/typical-nginx-configurations/
更多详情参看关于如何配置的教程:
http://wiki.nginx.org/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.nginx.org/NginxInstall
nginx -s [ stop | quit | reopen | reload ]

3. nginx的配置:
上面介绍了,首先nginx和Apache类似通过各种模块module可以对服务器的功能进行丰富的扩展。同样也是类似通过nginx.conf文件可以对进行核心配置或者针对各种模块的配置。
http://wiki.nginx.org/NginxModules
具体如何配置通常还是取决于,准备使用nginx来做什么,几个很好的关于配置的参考资料:
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/
http://wiki.nginx.org/NginxConfiguration

0) 首先我们展示一下默认的nginx.conf文件中各个部分:
nginx.conf配置文件中,针对不同的模块使用大括号包括起来,很方便地进行配置;
一个非常好的教程可以参看:
http://wiki.nginx.org/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.kovyrin.net/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://sameerparwani.com/posts/nginx-as-a-front-end-to-apache/
常见的情况是,使用nginx直接处理静态的请求响应;对于动态的才pass给服务器,而且对服务器的响应结果进行缓存。

3)nginx做为多台server的负载均衡作用:
http://sameerparwani.com/posts/load-balancing-with-nginx/

4)另外一个常见的应用就是直接使用nginx代替了Apache,使用Linux+Nginx+MyQL+PHP的stack。
这个时候首先需要以FastCGI的方式安装PHP(需要一个FastCGI的process server),然后配置nginx支持PHP。
具体参考相关的专题介绍。

5)更多的配置介绍参看
http://wiki.nginx.org/NginxModules
各个模块的配置和configure教程。[/size]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值