您的位置:首页 > 运维架构 > Nginx

Nginx安装及配置简介

2014-04-03 11:16 555 查看
Nginx ("engine x") 是一款高性能的,轻量级的HTTP Web 服务器 和 反向代理服务器及电子邮件 IMAP/POP3/SMTP 代理服务器。

Nginx 是由俄罗斯的程序设计师 Igor Sysoev 所开发,为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过四年多时间了,Igor 将源代码以类BSD许可证的形式发布。自 Nginx 发布四年来,Nginx 已经因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。Nginx 超越Apache的高性能和稳定性,使得国内使用 Nginx 作为
Web 服务器的网站也越来越多。

目前国内各大门户网站已经部署了Nginx,如新浪、网易、腾讯等;新近发现 Nginx 技术在国内日趋火热,越来越多的网站开始应用部署Nginx。

一、首先去官网下载 nginx的Windows版本,官网下载:http://nginx.org/download

下载到软件包后,解压 nginx-1.0.11.zip 包到你喜欢的根目录,并将目录名改为nginx。

然后,执行下列操作:

cd nginx

start nginx

这样,nginx 服务就启动了。打开任务管理器,查看 nginx.exe 进程,有二个进程会显示,占用系统资源,那是相当的少。然后再打开浏览器,输入http://127.0.0.1/  就可以看到nginx的欢迎页面了

nginx -s stop          // 停止nginx

nginx -s reload       // 重新加载配置文件

nginx -s quit          // 退出nginx

配置文件 
http://wiki.codemongers.com/NginxFullExample
#运行用户user nobody nobody;
#启动进程
worker_processes 5;
#全局错误日志及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及连接数上限
events {
#工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),epoll(高效模式,本例用的。适用Linux 2.6+,SuSE 8.2,),/dev/poll(高效模式,适用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
use epoll;
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include conf/mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';

log_format download '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" ' '"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size 10k;
large_client_header_buffers 4 4k;

#开启gzip模块,要求安装gzip 在运行./config时要指定
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#设定访问日志
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;

#设定负载均衡的服务器列表
upstream backserver {
#weigth参数表示权值,权值越高被分配到的几率越大 本例是指在同一台服务器,多台服务器改变ip即可
server 127.0.0.1:8081 weight=5;
server 127.0.0.1:8082;
server 127.0.0.1:8083;
}

#Deny access to any host other than (www).4535.com
server {
server_name _; #default
return 404;
}

#设定虚拟主机,默认为监听80端口
server {
listen 80;
server_name test.com www.test.com;
charset utf8;
#设定本虚拟主机的访问日志
access_log logs/test.com.log main;
#如果访问 /images/*, /js/*, /css/* 资源,则直接取本地文件,不用转发。但如果文件较多效果不是太好。
location ~ ^/(images|js|css)/ {
root /usr/local/testweb;
expires 30m;
}
#对 "/" 启用负载均衡
location / {
proxy_pass http://backserver; proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址,在运行./config 要指定,默认是不安装的。

location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";#是否要通过用户名和密码访问,测试时可以不加上。conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可
#auth_basic_user_file conf/htpasswd;
}
}
server{
#监听8088端口
listen 8088;
#服务名称
server_name m.yhd.com;
#对根的访问代理到8080端口
location / {
proxy_pass http://localhost:8080; }
#配置错误页面
error_page 404 http://localhost:8080/404.html; #日志
access_log logs/access.log main;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: