Nginx的基本配置
更新时间:2018-12-11 | 阅读量(1,129)
## 1.Nginx 创建虚拟主机
**1.创建一个虚拟主机**
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建一个虚拟主机(虚拟主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/blog; # 修改发布项目的目录
index index.html index.htm;
}
}
}
# 注意该配置文件的末尾不能有多余的空行
```
浏览器访问:http://www.liujun.com:8090/
**2.创建2个虚拟主机监听同一个端口**
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html)
index index.html index.htm;
}
}
# 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com)
server {
listen 8090;
server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/new; # 修改发布项目的目录(new项目中只有一个index.html)
index index.html index.htm;
}
}
}
# 注意该配置文件的末尾不能有多余的空行
```
浏览器访问:http://www.liujun.com:8090/ 和 http://www.new.liujun.com:8090/ ,
如下图,已经实现在一台服务器中发布多个网站( 一个端口对应多个域名 )

3.创建多个虚拟主机
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html)
index index.html index.htm;
}
}
# 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com)
server {
listen 8090;
server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.new.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/new; # 修改发布项目的目录(new项目中只有一个index.html)
index index.html index.htm;
}
}
# 3.创建第三个虚拟主机(虚拟主机名称为:www.liujun.com)
server {
listen 9090;
server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/music; # (music项目中只有一个index.html)
index index.html index.htm;
}
}
}
# 注意该配置文件的末尾不能有多余的空行
```
浏览器访问:http://www.liujun.com:9090/
如下图,已经实现在一台服务器中发布多个网站( 一个域名 对应多个端口 , 一个端口对应多个域名 )

**4.配置文件的抽取( 引入外部的配置文件 )**
**nginx.conf配置文件**
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建第一个虚拟主机( 引入外部的配置文件 )
include /usr/local/etc/nginx/nconfig/blog.conf; # 每一个语句的尾部不能少了分号
#include /usr/local/etc/nginx/nconfig/*.conf;
# 2.创建第二个虚拟主机(
server {
listen 8090;
server_name www.new.liujun.com;
location / {
root /usr/local/var/www/webapps/new;
index index.html index.htm;
}
}
# 3.创建第三个虚拟主机
server {
listen 9090;
server_name www.liujun.com;
location / {
root /usr/local/var/www/webapps/music;
index index.html index.htm;
}
}
}
# 注意该配置文件的末尾不能有多余的空行
```
**blog.conf 配置文件**
```js
# 1.创建第一个虚拟主机(虚拟主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com; # 本地测试要在hosts中添加对 www.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/blog; # 修改发布项目的目录(blog项目中只有一个index.html)
index index.html index.htm;
}
}
```
> mkdir 新建一个文件夹 ; touch 文件一个文件 ;vim 编辑文件
执行的效果:

## 2.Nginx 动静态资源分离
###1.配置反向代理
1.基本的配置
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建虚拟主机(主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com;
location / {
# 2.把所有对 www.liujun.com:8090 的请求转发到 https://www.taobao.com
proxy_pass https://www.taobao.com;
}
}
}
```
> 这种反向代理可以实现 把 HTTP 转换成更安全的 HTTPS 的方案
**2.其它配置**
代理其他的服务器
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建虚拟主机(主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com;
location / {
# 2.把所有对 www.liujun.com:8090 的请求转发到 https://news-at.zhihu.com
proxy_pass https://news-at.zhihu.com;
# 3.其它配置
proxy_redirect off; # off使 location 或者 refresh 字段维持不变
# 默认情况下反向代理是不会转发请求中的 Host 头部的。如果需要转发,那么必须加上下面的配置
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
```
浏览器访问:http://www.liujun.com:8090/api/4/news/latest 反向代理后变成请求 https://news-at.zhihu.com/api/4/news/latest,最终获取到结果.
> 地址栏没有变还是http://www.liujun.com:8090/api/4/news/latest
代理同一台服务器的同一个端口
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 1.创建虚拟主机(主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com;
location / {
# 2.把所有对 www.liujun.com:8090 的请求转发到 www.new.liujun.com:8090
proxy_pass http://www.new.liujun.com:8090;
# 3.其它配置
proxy_redirect off; # off使 location 或者 refresh 字段维持不变
# 默认情况下反向代理是不会转发请求中的 Host 头部的。如果需要转发,那么必须加上下面的配置
# 参考文章:https://blog.csdn.net/felix_yujing/article/details/51682655
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 2.创建第二个虚拟主机(虚拟主机名称为:www.new.liujun.com)
server {
listen 8090;
server_name www.new.liujun.com; # 本地测试要在hosts中添加对 www.new.liujun.com 的解析
location / {
root /usr/local/var/www/webapps/new; # (new项目中只有一个index.html)
index index.html index.htm;
}
}
}
```
浏览器访问:http://www.liujun.com:8090/ 显示的是new项目的网页
> 地址栏没有变还是http://www.liujun.com:8090/
###2.配置动静态资源的分离
1.动态资源 :`api接口 、文件 、压缩包 、软件 、 .jsp 、.php 、.shtml 、.ejs 等等`
2.静态资源:`html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css`
```js
# 修改配置文件需要重启服务器
user liujun liu;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
# 创建虚拟主机(主机名称为:www.liujun.com)
server {
listen 8090;
server_name www.liujun.com;
# 1.动态资源的加载。
location / {
# 把所有对 www.liujun.com:8090 的动态资源的请求转发到 https://news-at.zhihu.com
proxy_pass https://news-at.zhihu.com;
# 其它配置
proxy_redirect off;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# 2.静态资源的加载。匹配以.html 、.htm 、.gif、.jpg、.jpeg结尾的请求
# ( ~ 表示匹配 URI 时是字母大小写敏感的; ~* 大小写不敏感; \. 是转译.)
location ~* \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /usr/local/var/www/webapps/new; # (new项目中只有一个index.html)
# expires定义用户浏览器缓存的时间为3天,如静态页不常更新,可以设更长
expires 30d;
}
}
}
```
> 上面两个 location 配置快是并列关系
1)浏览器访问:http://www.liujun.com:8090 反向代理到:https://news-at.zhihu.com 这个网站( 地址栏不变 )
2)浏览器访问:http://www.liujun.com:8090/api/4/news/latest 反向代理到 :https://news-at.zhihu.com/api/4/news/latest 这个api 接口
3)浏览器访问:http://www.liujun.com:8090/index.html 直接获取本地的服务器 `/usr/local/var/www/webapps/new; ` 路径下的静态资源。