1、安装nginx
2、打开nginx
nginx主要的配置都在这个叫做nginx.conf的文件里,接下来打开他
主要两大部分组成:events、http
events:主要配置连接数等信息
http:主要配置反向代理负载均衡
假设有多个不同端口的相同服务,外部只需要登录一个ip地址就可以直接访问,现在可以通过以下配置直接通过localhost就可以访问运行的多个服务,权重越高访问频率越高,注意http服务默认端口是80,https默认端口是443
upstream landada {
ip_hash;
server 127.0.0.1:8802 weight=6;#负载均衡 权重6
server 127.0.0.1:8801 weight=4;#负载均衡 权重4
server 127.0.0.1:8800 weight=2;#负载均衡 权重2
}
server {
listen 80;
server_name localhost;
location / {
root test;
index test.html;
proxy_pass http://landada;#反向代理
}
}
使用nginx代理静态资源
这里演示的是代理的阿里oss资源,首先在oss上建立bucket并允许对外访问
直接访问 https://地址.oss-cn-qingdao.aliyuncs.com/test/updataserver/328572db61734bd2803bf656c45b81ec.png
是可以直接下载信息的,现在需要使用nginx做一层代理,配置如下
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location ^~ /api {
root html;
index index.html index.htm;
}
location ^~ /update/ {
proxy_pass https://地址.oss-cn-qingdao.aliyuncs.com/test/updataserver/;
}
}
按照上方配置完成后,访问 http://localhost/update/328572db61734bd2803bf656c45b81ec.png 是可以正常下载的
基本操作命令
start nginx 启动nginx
nginx -s stop 强行停止nginx
nginx -s quit 停止nginx
nginx -s reload 重启nginx(修改配置文件需要重启)
nginx -c conf/nginx.conf 创建pid文件