nginx服务配置
nginx出发点就是一个http的服务,一个纯粹做http协议的服务
windows安装可参考以下
Mac安装
- 安装
brew install nginx
- 查看版本
nginx -v
- 安装位置
/usr/local/etc/nginx
- 启动
sudo nginx
- 查看 nginx 是否启动成功
在浏览器中访问 http://localhost:8080,如果出现如下界面,则说明启动成功.
- 关闭nginx
sudo nginx -s stop
- 重新加载nginx
sudo nginx -s reload
修改hosts文件配置本地域名
hosts位置:
- Windows C:\windows\system32\drivers\etc\hosts
- Mac /private/etc/hosts
- Ubuntu /etc/hosts
vim hosts
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 test.com
保存以上配置即可,127.0.0.1 test.com 在浏览中输入www.test.com域名,就可访问本地指定的网站,仅限于本地。
注意
Nginx中,要做好conf配置,让这些域名有所访问的对象,例如下面Nginx配置缓存处的test.com指向http://127.0.0.1:3010
查看是否配置成功 可以打开cmd ping 一下配置的余名,例如上面配置的
ping test.com
PING test.com (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.047 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.095 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.084 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.055 ms
nginx配置缓存
- levels 是否要创建二级文件夹
- keys_zone=my_cache:10m 代理缓存查找一个缓存之前要有个地方保存,一个url对应的缓存保存在哪个地方,这个关系是存在内存里的,这里要声明一个内存大小进行保存,my_cache是缓存的名字,在每个server里面可以去设置
修改conf配置,文件目录了 /usr/local/etc/nginx/servers/
vim nginx-cache.conf
proxy_cache_path /var/cache levels=1:2 keys_zone=my_cache:10m;
server {
listen 80;
server_name test.com;
location / {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:3010;
proxy_set_header Host $host; # 设置浏览器请求的host
}
}
nginx-cache.js
- 以下s-maxage会代替max-age,只有在代理服务器(nginx代理服务器)才会生效
- 用来指定在发送一个请求时,只有在Vary指定的http的headers是相同的情况下,才会去使用缓存,例如User-Agent,IE、Firefox打开这个页面,CDN/代理服务器就会认为这是不同的页面,将会使用不同的缓存
const http = require('http');
const fs = require('fs');
const port = 3010;
const wait = seconds => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, seconds);
})
}
http.createServer((request, response) => {
console.log('request url: ', request.url);
if (request.url === '/') {
const html = fs.readFileSync('nginx-cache.html', 'utf-8');
response.writeHead(200, {
'Content-Type': 'text/html',
});
response.end(html);
} else if (request.url === '/data') {
response.writeHead(200, {
'Cache-Control': 'max-age=20, s-max-age=20',
'Vary': 'Test-Cache-Val'
});
wait(3000).then(() => response.end("success!"));
}
}).listen(port);
console.log('server listening on port ', port);
ngxin-cache.html
<html>
<head>
<meta charset="utf-8" />
<title>nginx-cache</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.4/fetch.min.js"></script>
</head>
<body>
<div>
this is nginx-cache, and data is: <span id="data">请等待,数据获取中...</span>
</div>
<script>
fetch('/data', {
headers: {
'Test-Cache-Val': '123'
}
}).then((res => res.text())).then(text => {
document.getElementById('data').innerText = text;
});
</script>
</body>
</html>
以上就是关于nginx
代理服务器的实现实例,具体的Nginx
代理服务器缓存还是有很多的功能,比如通过一些脚本让缓存使用内存数据库搜索性能会更高,默认nginx
缓存是写在磁盘上的,读写磁盘效率是很低的,还可以通过设置cache key
等。
nginx部署https服务
生成public key
和private key
/usr/local/etc/nginx/certs
目录下执行以下命令
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem
Generating a 2048 bit RSA private key
...............................................................................+++
..............+++
writing new private key to 'localhost-privkey.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:
Email Address []:
基于上面的nginx-cache.conf
文件进行修改
proxy_cache_path /var/cache levels=1:2 keys_zone=my_cache:10m;
server {
listen 443 ssl; # https默认证书
server_name test.com;
# ssl on; # 开启ssl证书
ssl_certificate_key /usr/local/etc/nginx/certs/localhost-privkey.pem;
ssl_certificate /usr/local/etc/nginx/certs/localhost-cert.pem;
location / {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:3010;
proxy_set_header Host $host; # 设置浏览器请求的host
}
}
注意:
nginx: [warn] the “ssl” directive is deprecated, use the “listen … ssl”
http自动跳转https
proxy_cache_path /var/cache levels=1:2 keys_zone=my_cache:10m;
server {
listen 80 default_server;
listen [::]:80 default_server; # [::] 你的ip
server_name test.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl; # https默认证书
server_name test.com;
# ssl on; # 开启ssl证书
ssl_certificate_key /usr/local/etc/nginx/certs/localhost-privkey.pem;
ssl_certificate /usr/local/etc/nginx/certs/localhost-cert.pem;
location / {
proxy_cache my_cache;
proxy_pass http://127.0.0.1:3010;
proxy_set_header Host $host; # 设置浏览器请求的host
}
}
实现http2协议
http2
目前只能在https
下面才可以
- 优势:
- 信道复用
- 分帧传输
- Server Push http/1.1协议里是客户端主动请求,服务才会响应,
http2.conf
server {
listen 443 ssl http2;
server_name http2.test.com;
http2_push_preload on;
ssl_certificate_key /usr/local/etc/nginx/certs/localhost-privkey.pem;
ssl_certificate /usr/local/etc/nginx/certs/localhost-cert.pem;
location / {
proxy_pass http://127.0.0.1:30100;
proxy_set_header Host $host;
}
}
connection.js 基于http/1.1长链接示例修改
const http = require('http');
const fs = require('fs');
const port = 30100;
http.createServer((request, response) => {
console.log('request url: ', request.url);
const html = fs.readFileSync('./connection.html', 'utf-8');
const img = fs.readFileSync('./test_img.jpg');
if (request.url === '/') {
response.writeHead(200, {
'Content-Type': 'text/html',
'Connection': 'close',
'Link': '</test.jpg>; as=image; rel=preload',
});
response.end(html);
} else {
response.writeHead(200, {
'Content-Type': 'image/jpg'
});
response.end(img);
}
}).listen(port);
console.log('server listening on port ', port);
connection.html
<html>
<head>
<meta charset="utf-8" />
<title>http2-connection</title>
<style>
img {
width: 100px;
height: 100px;
}
</style>
</head>
<body>
1
<img src="/test1.jpg" alt="" />
2
<img src="/test2.jpg" alt="" />
3
<img src="/test3.jpg" alt="" />
4
<img src="/test4.jpg" alt="" />
5
<img src="/test5.jpg" alt="" />
6
<img src="/test6.jpg" alt="" />
7
<img src="/test7.jpg" alt="" />
8
<img src="/test8.jpg" alt="" />
</body>
</html>
运行效果,基于http2协议复合浏览器同域策略都在一个TCP上复用
测试http2性能的网站 https://http2.akamai.com/demo/http2-lab.html