nginx 快速学习

NGINX学习

参考B站视频 BV1F5411J7vK、BV1Bx411Z7Do

介绍

Nginx(engine x)是一个高性能的HTTP和反向代理web服务器,同时也提供IMAP/POP3/SMTP服务。

高性能:响应快 并发高

作用

① HTTP代理,反向代理:作为web服务器最常用的功能之一,尤其是反向代理

  • 正向代理
    vpn:代理服务器帮你请求外网资源 。代理客户端的就是正向代理
  • 反向代理
    代理服务端

正向代理隐藏真实的客户端,反向代理隐藏真实的服务端

② 负载均衡

③ 动静分离

下载

官网:https://nginx.org/en/download.html

文件

解压安装包之后

  • nginx.conf

    删除掉注释之后不到三十行:
powershell 复制代码
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;  # 搭配上面的80,localhost80访问到
        location / { # 默认的/路径
            root   html;  # 这里的html是文件
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html { # 出错时的错误页
            root   html;
        }
    }
}

补充说明 location / 的root
root html解释:安装路径为默认根路径,其下面的html文件

- location优先级问题

location / 匹配所有路径,访问的时候如果是localhost:80/b或者localhost:80/a也会到这里。但这个是最弱的级别

location = /a {}是优先级最高的,访问路径必须完全等于/a,localhost:80/a

location ^~ /a {}匹配优先级次高的,只要包含/a就可以,localhost:80/a/b

location ~ /\w {}优先级第三高,正则匹配。\w匹配的是数字字母下划线三种类型。

同一优先级,按书写顺序匹配。

  • 反向代理小技巧
powershell 复制代码
访问 localhost:80/a时,代理到 http://192.168.0.12:80/a
location /a {
  proxy_pass http://192.168.0.12:80;
}

访问 localhost:80/a时,代理到 http://192.168.0.12:80,这里的区别是没有/a
location /a/ {
  proxy_pass http://192.168.0.12:80/;
}

常用命令

powershell 复制代码
whereis nginx  # 查看安装到哪里了  我安装到 /usr/local/nginx
cd /usr/local/nginx/sbin/
./nginx # 启动
./nginx -s stop # 停止
./nginx -s quit # 安全退出
./nginx -s reload # 重新加载配置文件,但凡修改了配置文件都要用这个命令
ps aux|grep nginx # 查看nginx教程
相关推荐
还是大剑师兰特3 天前
解决nginx错误:http://localhost:7000正常,http://localhost:7000/map 报错404
nginx·大剑师
xing-xing3 天前
Docker容器中Nginx站点根目录网页配置访问
nginx·docker
PC2005-cloud3 天前
Nginx 防盗链配置实战:用 referer 模块保护网站静态资源
nginx
PHP实战开发录3 天前
Nginx上传大文件为什么报413
nginx·php·开发
Lsetea4 天前
Nginx报No required SSL certificate was sent:mTLS客户端证书排查
运维·nginx·https·ssl·openssl
szephyr5 天前
HTTPS 证书申请与自动续期:acme.sh + Let‘s Encrypt 完整记录
nginx·https·acme.sh·证书续期
Lsetea5 天前
部署证书后提示“证书名称与输入不匹配”:Safari 报错的三层排查
nginx·https·safari·ssl证书·openssl
打工仔折腾 AI5 天前
FastAPI 从本机到生产服务器:Nginx+Gunicorn+Uvicorn 完整部署实录
人工智能·后端·python·nginx·fastapi·gunicorn
Linux运维技术栈6 天前
如何避免绕过CDN的攻击?基于 CDN 回源请求头鉴权的防御实操与安全性剖析
nginx·安全·cdn
Lsetea6 天前
OpenSSL快速生成域名证书:含SAN的自签命令、验证与排错
nginx·https·ssl证书·openssl·自签证书