02-conf

1. nginx.conf

  • 格式像json,但不是json。每行以;结尾
image-20220713094015217
image-20220713094023588

1. 403

image-20220713094031233
# nginx.conf默认用户
user nginx;

# nginx.conf首行加入
user root;
# 将错误信息完整打印
systemctl status nginx.service -l

2. 虚拟主机

  • server.listen一样,server_name不一致。可代表不同主机
  • server_name域名可以配置多个,空格隔开
  • 请求时,Request Headers 中的 Host 携带了域名
# nginx test
192.168.10.101 nginx.test1
192.168.10.102 nginx.test2
user  root;
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    # server表示一台主机
    # 虚拟主机1
    server {
        # 端口
        listen       80;

        # 域名
        server_name  nginx.test1;

        location / {
            # 路径
            root   /root/html/li;
            # 默认页
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    # 虚拟主机2(一台物理主机虚拟出两个域名对应的虚拟主机)
    server {
        listen       80;
        server_name  nginx.test2;

        location / {
            root   /root/html/song;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

}















 



 


 

 
 
 
 
 
 







 
















  • http协议的规定
image-20220713094111228

Nginx支持三种类型的虚拟主机配置

  • 基于域名的虚拟主机(servername)
  • 基于ip的虚拟主机 (一块主机绑定多个ip地址)
  • 基于端口的虚拟主机(同一ip不同的端口)
http {
	server {
		# 表示一个虚拟主机
	}
}

1. index

  • 访问root目录时,默认访问哪个静态资源

3. 代理

1. 基于反向代理负载均衡

1. 反向代理

# 尝试代理google
location / {
    proxy_pass http://google.com
}
  • 301:跳转
  • 307:只能跳转到域名内
image-20220713152414341
location / {
    proxy_pass https://google.com
}
image-20220713152427955

2. 负载均衡

image-20240823103234217
1. 轮循权重
  • nginx-80nginx-88nginx-99进行代理
server {
    listen       88;
    server_name  localhost;

    location / {
        root   /www/88;
        index  index.html index.htm;
    }
}
server {
    listen       99;
    server_name  localhost;

    location / {
        root   /www/99;
        index  index.html index.htm;
    }
}
  • 启动后可以单独访问
# -c 从nginx根目录开始
./nginx -c conf/nginx-88.conf
server {
    listen       80;
    server_name  localhost;

    location / {
        proxy_pass http://listao.sites;
    }
}

upstream listao.cns {
    server 192.168.10.101:80 weight=1;
    server 192.168.10.102:80 weight=10;
}





 



 
 
 
 

2. 安全问题

1. 反向代理

  • 用户和Proxy(nginx-80)直接https连接,中间无法篡改
image-20240823103234217

2. 正向代理

  1. 用户自己找的代理服务器,建立http连接,可能被篡改
  2. 301重定向,能解决正常的正向代理
image-20240823103901368

4. https

两端做非对称加密

  1. 免费的签名没有保障
  2. 签名会提供证书,同时提供嵌在OS内部的publicKey
  3. 签名是基于域名的

1. aliyun申请SSL

image-20240823105919716
image-20240823110008167
image-20240823110253979
image-20240823110437550
image-20240823155148361

2. nginx_ssl

  • 直接穿透,不影响 https
image-20240823160212386
    # RuoYi官网的配置
    server {
        listen 18082 ssl;
        server_name listao.cn;

        # 填写证书文件绝对路径
        ssl_certificate cert/listao.cn.pem;
        # 填写证书私钥文件绝对路径
        ssl_certificate_key cert/listao.cn.key;


        # 指定前端项目所在的位置
        location / {
            root /etc/nginx/ruoyi;
            try_files $uri $uri/ /index.html;
            index index.html index.htm;
        }

        error_page 500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /prod-api/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://ruoyi:7071/;
        }

        location /api-docs/ {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://ruoyi:7071;
        }

    }


 
 


 

 































  • 域名申请的证书、访问域名不一致
cWxKR1R1eVJkYjRiaVlLQzc5TWVQQ2pSUGlnUTNKMGl2Ykk0MW1iZ1RnPT0=
image-20240823160616258
image-20240823160755807

3. listao_doc

  • Mixed Contenthttps文档内容中包含http嵌套,自动将http升级为https链接
image-20240823140543549
    # HTTPS server
    server {
        listen       443 ssl;
        server_name  listao.cn;

        # 填写证书文件绝对路径
        ssl_certificate cert/listao.cn.pem;
        # 填写证书私钥文件绝对路径
        ssl_certificate_key cert/listao.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location / {
            root   /etc/nginx/html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


 
 
 
 
 
 
 
 
 
 
 
 
 











4. 增加module

  • 编译时报错
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:98
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
  • 重新编译,增加ssl模块
./configure --with-http_stub_status_module --with-http_ssl_module
  • make执行完之后,不要执行install,会覆盖之前的nginx配置
  • 源码包下,替换objs/nginx可执行文件即可

1. docker_nginx模块

  • 进行容器,查看nginx已经加载的模块
[root@iZ2zefbcegrwf36h107xhaZ ~]# docker exec -it nginx /bin/bash

root@8468db267167:/# nginx -v
nginx version: nginx/1.23.4
root@8468db267167:/# nginx -V
nginx version: nginx/1.23.4
built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
built with OpenSSL 1.1.1n  15 Mar 2022
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules
--conf-path=/etc/nginx/nginx.conf
--error-log-path=/var/log/nginx/error.log
--http-log-path=/var/log/nginx/access.log
--pid-path=/var/run/nginx.pid
--lock-path=/var/run/nginx.lock
--http-client-body-temp-path=/var/cache/nginx/client_temp
--http-proxy-temp-path=/var/cache/nginx/proxy_temp
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
--http-scgi-temp-path=/var/cache/nginx/scgi_temp
--user=nginx
--group=nginx
--with-compat
--with-file-aio
--with-threads
--with-http_addition_module
--with-http_auth_request_module
--with-http_dav_module
--with-http_flv_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_mp4_module
--with-http_random_index_module
--with-http_realip_module
--with-http_secure_link_module
--with-http_slice_module
--with-http_ssl_module
--with-http_stub_status_module
--with-http_sub_module
--with-http_v2_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_realip_module
--with-stream_ssl_module
--with-stream_ssl_preread_module
--with-cc-opt='-g -O2 -ffile-prefix-map=/data/builder/debuild/nginx-1.23.4/debian/debuild-base/nginx-1.23.4=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC'
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
 



 





 

























 
 










5. 动静分离

location / {
    proxy_pass http://127.0.0.1:8080;
}

 

1. location

location /css {
    root /usr/local/nginx/static;
    index index.html index.htm;
}

location /images {
    root /usr/local/nginx/static;
    index index.html index.htm;
}

location /js {
    root /usr/local/nginx/static;
    index index.html index.htm;
}
 




 




 



前缀释义
没有前缀匹配以指定模式开头的location
=精准匹配,不是以指定模式开头
~正则匹配,区分大小写
~*正则匹配,不区分大小写
^~非正则匹配,匹配以指定模式开头的location
# 匹配以/开始,()里任意目录
location ~*/(css|images|js) {
    root /usr/local/nginx/static;
    index index.html index.htm;
}

 



location匹配顺序

  • 所有类型location存在时,=匹配 > ^~匹配 > 正则匹配 > 普通(最大前缀匹配)
  • 多个location直接按书写顺序匹配,成功后就不会继续往后面匹配

2. root alias

  • root:设置根目录。location + uri
  • alias:将匹配成功部分alias替代

  1. alias不能使用rewritebreak(具体原因不明)。alias指定的目录后面必须要加上/符号
    • location最后不带/alias后面加不加/不影响
    • location最后带/alias必须要加上/。如果不加上/,访问就会失败!
  2. root目录配置中,location后面带不带/,都不会影响访问

6. Url_Rewrite

  • Apache要比Nginx的rewrite稳定的多
  • rewrite是实现URL重写的关键指令,根据regex(正则表达式)部分内容,重定向到replacement,结尾是flag标记
  • rewrite参数的标签段位置:server,location,if
rewrite    <regex>    <replacement>    [flag];
关键字      正则        替代内容         flag标记
  • 关键字:其中关键字error_log不能改变
  • 正则:perl兼容正则表达式语句进行规则匹配
  • 替代内容:将正则匹配的内容替换成replacement
  • flag标记:rewrite支持的flag标记
    • last:本条规则匹配完成后,继续向下匹配新的location URI规则
    • break:本条规则匹配完成即终止,不再匹配后面的任何规则
    • redirect:返回302临时重定向,浏览器地址栏会显示跳转后的URL地址
    • permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址
http://localhost:8080/account/list?pageNum=1
http://localhost:8080/account/list#pageNum=1

# 优雅方式
list/1.html
http://localhost:8080/account/list-2.html

# 经过nginx_rewrite
http://localhost:8080/account/list?pageNum=2
rerewrite ^/account/list-([0-9]+).html /account/list?pageNum=$1 last;

rerewrite ^/account/(.+)/list-([0-9]+).html /account/list?pageNum=$2&xxoo=$1 last;
rewrite ^/account/login.html$ /account/login last;

rewrite ^/account/(.+).html$ /account/list?pageNum=$1 last;

1. 短网址

listao.cn/ooxx1 => listao.cn/userCtl?pageNum=1

  1. nginx请求一个公共接口,ooxx1为key,获取userCtl?pageNum=1,进行重写uri
  2. nginx直连DB,ooxx1为key,获取userCtl?pageNum=1,进行重写。
    • 还可以直连kafka记录log,Server异步消费kafka进行分析