dream789 发表于 2018-11-11 06:58:51

nginx的简单使用

#yum -y --nogpgcheck localinstall nginx-0.6.39-5.el5.i386.rpm  vim /etc/nginx/nginx.conf
  1.修改nginx的默认网页位置
  location / {
  root   /web;
  indexindex.html index.htm;
  }
  2.测试nginx添加虚拟主机(可定义多个主机,其中listen可为端口,ip,ip+端口,主机头)
  server {
  listen 8080;
  location / {
  root    /web;
  index index.html;
  }
  2.nginx的认证(需要使用apache的htpasswd命令)
  # yum install -y httpd
  #vim /etc/nginx/nginx.conf
  location / {
  root   /web;
  auth_basic“You needakey”   #提示语
  auth_basic_user_file/etc/nginx/htpasswd#指定读取的授权文件路径
  indexindex.html index.htm;
  }
  创建htpasswd的文件
  touch /etc/nginx/htpasswd
  建立用户tom,并给与密码(不支持md5算法)
  htpasswd -cd /etc/nginx/htpasswdtom
  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;
  indexindex.html index.htm;
  }
  }

页: [1]
查看完整版本: nginx的简单使用