zwd 发表于 2018-11-11 08:33:58

Nginx常用配置实例

  随着Nginx在企业中的广泛应用,作为一个运维工程师或者系统管理员,很有必要掌握Nginx的使用和配置,本人将工作中常用的一些配置实例与大家分享,希望对大家有一些帮助。
  一 虚拟主机配置
  1 基于端口的虚拟主机:
  # vim /opt/nginx/conf/nginx.conf
  server {
  listen8080;
  location / {
  root /opt/nginx/html/vm01;
  index index.html index.htm;
  }
  }
  server {
  listen8081;
  location / {
  root /opt/nginx/html/vm02;
  index index.html index.htm;
  }
  }
  # mkdir /opt/nginx/html/vm01
  # mkdir /opt/nginx/html/vm02
  # vim /opt/nginx/html/vm01/index.html
  this is vm01
  # vim /opt/nginx/html/vm02/index.html
  this is vm02
  # service nginx restart
  # netstat -tnlp |grep 808
  tcp00 0.0.0.0:8080    0.0.0.0:*   LISTEN26482/nginx.conf
  tcp00 0.0.0.0:8081    0.0.0.0:*   LISTEN26482/nginx.conf
  # elinks http://192.168.1.3:8080
  # elinks http://192.168.1.3:8081
  2 基于IP的虚拟主机(在原来做基于端口的虚拟主机上更改):
  # ifconfig eth0 192.168.1.2
  # ifconfig eth0:0 192.168.1.3
  # vim /opt/nginx/conf/nginx.conf
  server {
  listen192.168.1.2:80;
  location / {
  root /opt/nginx/html/vm01;
  index index.html index.htm;
  }
  }
  server {
  listen192.168.1.3:80;
  location / {
  root /opt/nginx/html/vm02;
  index index.html index.htm;
  }
  }
  # service nginx restart
  # elinks http://192.168.1.2
  # elinks http://192.168.1.3
  3 基于相同IP的不同主机头的虚拟主机:
  # vim /etc/hosts
  192.168.1.3   www.abc.com
  192.168.1.3   www.bcd.com
  192.168.1.3   www.def.com
  # vim /opt/nginx/conf/nginx.conf
  server {
  listen80;
  server_name www.abc.com;
  location / {
  root /opt/nginx/html/vm01;
  index index.html index.htm;
  }
  }
  server {
  listen   80 default;
  server_name www.bcd.com;
  location / {
  root /opt/nginx/html/vm02;
  index index.html index.htm;
  }
  }
  # service nginx restart
  # elinks www.bcd.com
  # elinks www.abc.com
  二 负载均衡
  nginx 的 upstream目前支持 4 种方式的分配 (后两种属于第三方调度)
  1)、轮询(默认)
  以顺序循环将服务请求分配到集群中的内容服务器上,所有服务器地位平等,不考虑服务器上实际的连接数和系统负载。
  2)、weight
  循环方式调度,但在循环中给每个内容服务器分配指定权重的连接,从而充分考虑各内容服务器处理能力之间的差异。
  3)、ip_hash
  每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
  4)、fair
  这种算法可以依据页面大小和加载时间长短智能低进行负载均衡。需要单独安装upstream_fair模块。
  4)、url_hash
  此方法按照访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。
  配置如下:
  worker_processes 1;
  events {
  worker_connections 1024;
  }
  http{
  upstream gupt12 {
  server 192.168.1.2:80; 这里可以写多个源服务器的IP地址。
  server 192.168.1.3:80;
  }
  server {
  listen 8080;
  location / {
  proxy_pass http://gupt12;
  }
  }
  }
  在上面这个配置实例中,先定义了一个负载均衡组gupt12,然后在location部分通过“proxy_pass http://gupt12”实现负载调度功能,其中proxy_pass指令用来指定代理
  服务器的后端服务器地址和端口,地址可以是主机名或者IP地址,也可以是通过upstream指定设定的负载均衡组名称。
  三 Nginx的防盗链
  简单的防盗链如下:
  location ~* \.(gif|jpg|png|swf|flv)$ {
  valid_referers none blocked www.51cto.com www.baidu.com;
  if ($invalid_referer) {
  rewrite ^/ http://www.51cto.com/403.html;
  #return 404;
  }
  }
  第一行:gif|jpg|png|swf|flv
  表示对gif、jpg、png、swf、flv后缀的文件实行防盗链
  第二行:www.51cto.com www.baidu.com
  表示对www.51cto.com www.baidu.com这2个来路进行判断
  if{}里面内容的意思是,如果来路不是指定来路就跳转到错误页面,直接返回404也是可以的。

页: [1]
查看完整版本: Nginx常用配置实例