### 1.Nginx 安装
```
yum install nginx
```
* 因为都默认都占用80端口,所以开启服务前先关掉apache
```
[root@VM_0_11_centos www]# systemctl stop httpd.service
[root@VM_0_11_centos www]# service nginx start
Redirecting to /bin/systemctl start nginx.service
```
### 2.虚拟域名配置
* 打开配置文件 `/etc/nginx/nginx.conf`
* 可以看到,用户的自定义配置可以写在 `/etc/nginx/conf.d/*.conf`

* 所以我们在`/etc/nginx/conf.d/`创建`yijia.conf`.编写如下。
```
server {
listen 80;
server_name yijia.com;
root /data/www;
index index.html index.htm;
}
```
### 3.伪静态设置
* 只需要修改yijia .conf
```
server {
listen 80;
server_name yijia.com;
root /data/www;
index index.html index.htm;
location / {
rewrite ^(.*)\.htmp$ /index.html;
}
}
```

### 4.反向代理

* 使用`proxy_pass`进行转发至目标服务器。
* 修改yijia.conf
```
server {
listen 80;
server_name yijia.com;
root /data/www;
index index.html index.htm;
location / {
rewrite ^(.*)\.htmp$ /index.html;
#使用proxy_pass进行转发设置,转发至本机。
proxy_pass http://118.25.114.209;
}
}
server {
listen 80;
server_name proxyb.com;
location / {
rewrite ^(.*)\.htmp$ /index.html;
#使用proxy_pass进行转发设置,转发baidu。
proxy_pass http://www.baidu.com;
}
```
* 在客户机上设置`hosts`把`proxyb.com`指向`nginx`所在服务器。
* 然后访问`proxyb.com`会转发至百度。
### 5.nginx 负载均衡
* 在upstream中配置负载服务器组。
```
#负载均衡组
upstream web_server{
#本机
server 118.25.114.209;
#taobao
server 140.205.94.189;
}
server {
listen 80;
server_name yijia.com;
location / {
#转发到负载均衡组
proxy_pass http://web_server;
}
}
```
### 6.加权轮询
> 可以设置负载均衡组中服务器访问的权重。
> 使用weight设置权重。
```
upstream web_server{
#本机
server 118.25.114.209 weight=1 max_fails=2 fail_timeout=2;
#taobao
server 140.205.94.189 weight=3 ;
}
```

### 7.ip_hash负载均衡
> 按照IP的hash结果分配服务器。可以使同一个IP客户端用户固定访问某一台服务器,解决了session共享问题。
```
upstream web_server{
ip_bash;
#本机
server 118.25.114.209 weight=1 max_fails=2 fail_timeout=2;
#taobao
server 140.205.94.189 weight=3 ;
}
```