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

nginx的简单使用

2010-09-01 20:35 239 查看
[root@station8 ~]#yum -y --nogpgcheck localinstall nginx-0.6.39-5.el5.i386.rpm
vim /etc/nginx/nginx.conf
1.修改nginx的默认网页位置
location / {
root /web;
index index.html index.htm;
}
2.测试nginx添加虚拟主机(可定义多个主机,其中listen可为端口,ip,ip+端口,主机头)
server {
listen 8080;
location / {
root /web;
index index.html;
}
2.nginx的认证(需要使用apache的htpasswd命令)
[root@station8 ~]# yum install -y httpd
[root@station8 ~]#vim /etc/nginx/nginx.conf
location / {
root /web;
auth_basic “You need a key” #提示语
auth_basic_user_file /etc/nginx/htpasswd #指定读取的授权文件路径
index index.html index.htm;
}
创建htpasswd的文件
touch /etc/nginx/htpasswd
建立用户tom,并给与密码(不支持md5算法)
htpasswd -cd /etc/nginx/htpasswd tom
3.nginx的反向代理及负载均衡
Nginux HTTP Upstream 模块为后端的服务器提供简单的负载均衡,有以下几种分配方式:
a.轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
b.weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
c.ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
d.fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
e.url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

upstream myhttp{
server 192.168.0.66; #后端服务器1
server 192.168.0.42 weight=5; #后端服务器2
}
server {
listen 80;
server_name _;

location / {
root /web;
proxy_pass http://myhttp; #指定代理的服务(与上面设定的myhttp保持一致)
proxy_set_header X-REAL-IP $remote_addr;
#auth_basic "You need a key";
#auth_basic_user_file /etc/nginx/htpasswd;
index index.html index.htm;
}
}

本文出自 “一份存档” 博客,请务必保留此出处http://linuxbpm.blog.51cto.com/1823930/386188
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: