���Ľ�������л�����Ķ�
    
一、nginx常用命令
===========
1.Nginx启动
1  | nginx -c /etc/nginx/nginx.conf  | 
nginx -s stop 或者
nginx -s quit 或者
pkill -9 nginx
1  | 
  | 
nginx -s stop
1  | 
  | 
nginx -s reload
1  | 
  | 
nginx -t
1  | 
  | 
##Main段,定义全局属性
events {
          ##定义不同IO模型下的工作机制;
}
http { 
    ##定义作为web服务器的相关属性(还可以反向代理mail)
    server { 
        ##定义一个虚拟主机的属性,所有web服务必须定义成一个虚拟主机,与httpd不同
        location [option] uri { 
            ##定义一个URI的特性
            ##location中可以嵌套location的
            location [option] uri { 
                #嵌套location
            }
               if (condition) {
                ##定义URL重写
            }
        }
    }
    upstream  <Name> {
        ##将多个server结合在一起,实现负载均衡
    }
 }
1  | 
  | 
user www-data;
1  | 
  | 
worker_processes 8;
1  | 
  | 
error_log /var/log/nginx/error.log info;
1  | 
  | 
pid /var/run/nginx.pid;
1  | 
  | 
worker_rlimit_nofile 65535;
1  | 
  | 
use epoll;
1  | 
  | 
worker_connections 65535;
1  | 
  | 
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
1  | 
  | 
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
1  | 
  | 
upstream mysvr {
    #weigth参数表示权值,权值越高被分配到的几率越大
    #本机上的Squid开启3128端口
    server 192.168.8.1:3128 weight=5;
    server 192.168.8.2:80  weight=1;
    server 192.168.8.3:80  weight=6;
}  
1  | 设定虚拟主机用指令server,其中包括端口,主机名称,默认请求等设置。  | 
server {
    #侦听80端口
    listen       80;
    #定义使用www.xx.com访问
    server_name  www.xx.com;
    #设定本虚拟主机的访问日志
    access_log  logs/www.xx.com.access.log  main;
    #默认请求
    location / {
          root   /root;      #定义服务器的默认网站根目录位置
          index index.php index.html index.htm;   #定义首页索引文件的名称
          fastcgi_pass  www.xx.com;
          fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
          include /etc/nginx/fastcgi_params;
     }
    # 定义错误提示页面
    error_page   500 502 503 504 /50x.html; 
    location = /50x.html {
    root   /root;
    }
}
1  | 
  | 
proxy_pass http://www.hubwiz.com;
1  | 
  | 
upstream ixdba.net{
    ip_hash;
    server 192.168.12.133:80;
    server 192.168.12.134:80  down;
    server 192.168.12.135:8009  max_fails=3  fail_timeout=20s;
    server 192.168.12.136:8080;
}
1  | 
  | 
location = / {
   #规则A
}
location = /login {
   #规则B
}
location ^~ /static/ {
   #规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
   #规则D
}
1  | 那么产生的效果如下:  | 
rewrite ^/b/(.\*)\.html /play.php?video=$1 break; 
1  | 
  | 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
       server {
            location / {
                      #root   html;      #定义服务器的默认网站根目录位置
                      #index index.php index.html index.htm;   #定义首页索引文件的名称
                      proxy_pass http://www.hubwiz.com;
             }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
           }
         }
}
1  | 
  | 
nginx -s stop   
nginx -c /etc/nginx/Mynginx.conf 
1  | 
  | 
## Basic reverse proxy server ##
    upstream apachephp  {
        server ip:8080; #Apache
    }
    ## Start www.nowamagic.net ##
    server {
        listen 80;
        server_name  www.nowamagic.net;
        access_log  logs/quancha.access.log  main;
        error_log  logs/quancha.error.log;
        root   html;
        index  index.html index.htm index.php;
        ## send request back to apache ##
        location / {
            proxy_pass  http://apachephp;
                #……………………
}
}
1  | 
  | 
server {
        listen       80;
        server_name  www.hubwiz.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                      root   /root; 
                      index index.php index.html index.htm;  
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
      }
server {
        listen       80;
        server_name  www.baidu.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
                      root   /root;     
                      index index.php index.html index.htm; 
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
1  | 
  | 
server
{
  listen       8080;
  server_name www.hubwiz.com;
  index index.html index.htm index.php;
  root  /home/wwwroot;
     location / {
         resolver        192.168.8.88;
         proxy_pass      $scheme://$http_host$request_uri;
         proxy_buffers   256 4k;
 }
   access_log off;
}
1  | 
  | 
upstream backend{
      #定义负载均衡设备的Ip及设备状态
      server 127.0.0.1:9090 down; 
      server 192.168.1.12:8080 weight=2 ;
      server 192.168.1.13:6060 max_fails=3 fail_timeout=30s;
      server 1192.168.1.14:7070 backup;
}
server{
    #…………………………
   location /{
        proxy_pass http://backend;
        #…………………………
    }
}
1  | 
  | 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
       server {
            location /hubwiz {
                      rewrite (.*) http://www.hubwiz.com;
        }
           location /baidu {
                      rewrite (.*) http://www.baidu.com;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
       }
   }
}
`    
修改我们自己的配置文件,保存,重新加载nginx,然后测试,在地址栏给出的地址后面分别输入“/hubwiz”、“/baidu”,是不是会和我的结果一样呢?
输入“/hubwiz”转到汇智网,输入“/baidu”是不是转到百度了呢?
