> ## Content Index
> Fetch the complete content index at: https://blog.hacking.pub/llms.txt
> Use this file to discover other available public pages before exploring further.

# nginx 的三个基础用法
- URL: https://blog.hacking.pub/2016/10/29/nginx-de-liang-ge-ji-chu-yong-fa/
- Published: 2016-10-29T14:26:20.000Z
- Updated: 2016-10-29T14:32:46.000Z
- Author: chainhelen
- Tags: nginx, vps管理

##### 介绍

一句话：高性能的http和反向代理服务器

##### nginx 常用命令

nginx 启动  
nginx -s reload 重启  
nginx -t 配置文件是否正确  
ps -ef | grep nginx 查找出nginx进程号 使用kill -9 nginx 杀死关闭

##### 一台vps，但是希望搭建两个网站

ubuntu apt-get 安装 nginx或者源码make 三部曲安装  
在/etc/nginx/nginx.confg配置里面可以看见include /etc/nginx/conf.d/conf，那么在/etc/nginx/conf.d下创建任意文件.conf来配置域名

```
server {
    listen 80;
    server_name hacking.pub *.hakcing.pub;
    location / {
       proxy_pass http://127.0.0.1:3000;
    }
}
server {
    listen 80;
    server_name lisuliq.site *.lisuliq.site;
    location / {
       proxy_pass http://127.0.0.1:3001;
    }
}

```

表示nginx监听80端口，把域名hacking.pub 和 ＊.hakcing.pub 的请求转发给 127.0.0.1:3000端口处理，把lisuliq.site 和 \*.lisuliq.site转发给3001端口处理。

##### 作为反向代理

在选择图床的时候，本来是选择七牛的，连钱都付了。但是发现七牛的绑定域名的cdn服务，必须要求备案。  
我的vps是digitalocean，dns解析是cloudeflare，肯定备案是不成了。  
最终不得已选择了dropbox，而本身dropbox是被墙的，国内别人在访问blog的时候，那就跪了。  
可以做一个反向代理：

```
server {
    listen 80;
    server_name image.hacking.pub;
    location / {
        proxy_pass https://dl.dropboxusercontent.com;
    }
}

```

- 首先上传一张照片到dropbox，选择共享，获得共享链接[https://www.dropbox.com/s/o94x3qif0dqkpss/cjb.png?dl=0](https://www.dropbox.com/s/o94x3qif0dqkpss/cjb.png?dl=0&ref=blog.hacking.pub)
- 在markdown里面将这个链接改成[http://image.hacking.pub/s/o94x3qif0dqkpss/cjb.png?dl=0](http://image.hacking.pub/s/o94x3qif0dqkpss/cjb.png?dl=0&ref=blog.hacking.pub) 即可
- 原理就是，本地的图片http请求报文的host是image.hacking.pub，服务端接收后，根据nginx的规则，会相应地转发给dropbox
- 同理，等dropbox回传服务器相片流的时候，也会被nginx转发给本地，这样翻墙的动作被服务器做了

##### http代理

```
server {  
    resolver 8.8.8.8;  
    listen 8888;  
    access_log /var/nginx_log;  
    error_log /var/nginx_errorlog;  

    location / {  
        proxy_pass http://$http_host$request_uri;  
    }
}  

```

就是个http代理，但nginx是不支持 http中 connect 方法的，所以肯定不支持https代理的， 这个流程详细可以看我另一篇文章, go\_proxy。  
上面的参数解释：

- resolver是dns服务器地址
- listen是监听端口
- access\_log和error\_log是日志
- $http\_host 和 $request\_uri 可以看成是变量，表示原来报文里面相应的数据