一、nginx下载安装
nginx是一款高性能的http服务器和反向代理服务器
nginx官网:https://www.nginx.com/
下载地址:http://nginx.org/en/download.html
(linux版本点击nginx-1.19.1,windows版本点击带有windows的,pgp不知道什么东东)
下载解压nginx-1.18.0.zip,双击nginx-exe,浏览器运行localhost:端口(conf文件里面的listen),能看到welcome表示安装启动成功
二、nginx命令
查看帮助 nginx -h
显示版本信息 nginx -v
显示版本和配置项信息 nginx -V
重新打开日志文件 nginx -s reopen
设置nginx的前缀路径 nginx -p
启动
通过默认的配置文件启动 /nginx/sbin/nginx
指定配置文件启动 /nginx/sbin/nginx -c filepath
其他方式启动 service nginx start windows: nginx start 或者 nginx.exe
优雅退出nginx nginx -s quit windows: nginx.exe -s quit 或者 nginx.exe -s stop
快速停止 nginx -s stop windows:nginx.exe -s reload
停止(要先查看nginx的主进程号)
kill -quit 主进程号
kill -9 主进程号
kill -term 主进程号
pkill -9 nginx
service nginx stop 或者 nginx -s stop
平滑升级
1、编译安装新的可执行文件
2、执行kill -usr2旧版本nginx进程号
3、执行kill -winch旧版本nginx主进程号
三、nginx配置文件
1、配置文件主要区块
main(全局设置) 作用域是全局
events(nginx工作模式)
http
upstream(负载均衡服务器设置)
server(主机设置)
location(URL匹配)
2、配置文件示例
http{
upstream tuling1{
server 127.0.0.1:8081; #本地服务1
server 127.0.0.1:8082; #本地服务2
}
upstream tuling2{
server 127.0.0.1:8083; #本地服务3
server 127.0.0.1:8084; #本地服务4
}
server{
location / {
proxy_pass http://tuling; //配置后端接口路径
}
}
}
http{
upstream tuling{
server 127.0.0.1:8081; #本地服务1
server 127.0.0.1:8082; #本地服务2
server 127.0.0.1:8083; #本地服务3
}
server{
listen 80; //用户访问端口
server_name 192.168.1.72; //用户访问域名(要去host文件配置)
location /hello/ { //用户访问路径
proxy_pass http://127.0.0.1:8090/hello/; //本地idea项目访问路径(实际路径)
}
location /haha/ { //用户访问路径
proxy_pass https://www.baidu.com/; //实际路径
}
location /012/ { //访问路径
root html; //页面文件夹
index index.htm; //页面名称 所以这个静态文件在 /html/012/index.html
}
}
}
3、 原始配置文件 nginx.conf (有加一些中文说明)
#user nobody;
#一、头
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#二、工作模式
events {
worker_connections 1024;
}
# 三、剩余部分都是http的了
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
四、nginx代理模式
代理:给某个对象提供一个代理对象,并由代理对象控制源对象的引用
正向代理:代表了客户端去获取服务器资源
反向代理:代表了服务器,响应给用户
五、nginx负载均衡
在这里插入代码片
六、nginx限流
在这里插入代码片
七、nginx动静分离
在这里插入代码片
八、nginx热备
热备:不停机情况下对数据进行备份(就像热部署)
九、nginx安全认证
在这里插入代码片