nginx配置文件

关于 Nginx的核心配置文件nginx.conf

## 指定用户
user  nginx;
## 指定进线程数 一般核心数的两倍
worker_processes  auto;

## 日志记录
error_log  /var/log/nginx/error.log notice;
## 线程ID
pid        /var/run/nginx.pid;

# 以上统称为全局块
# worker_processes的数值越大,nginx的并发能力就越强
# error_log 代表nginx的错误日志存放的位置

events {
  ## 工作模型 日志中 看到
  use epoll;
    worker_connections  1024;
}

# events块
# worker_connections的数值越大,nginx的并发能力越强

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }
   # location块
   # root:将接受到的请求根据/usr/share/nginx/html去查找静态资源
   # index:默认去上述的路径中找到index.html或者index.htm
}
# server块
# listen:代表nginx监听的端口号
# localhost:代表nginx接收请求的IP


}

# http块
# include代表引入一个外部的文件 ->/mime.types中放着大量的媒体类型
# include /etc/nginx/conf.d/*.conf;->引入了conf.d目录下的以.conf为结尾的配置文件

img

基于nginx实现反向代理

  1. 准备一个目标服务器
  2. 启动服务器
  3. 编写nginx的配置文件,通过nginx访问服务器
server{
  listen 9000;
  server_name localhost;
  # 基于反向代理访问服务器
  location / {
    proxy_pass http://192.168.206.128:8080/;
  }
}

关于Nginx的location路径映射

优先级关系:
(location =) > (location/XXX/YYY/ZZZ) > (location ^~) > (location ,*) > (location/起始路径) > (location /)

# 1. = 匹配
location = / {
  # 精准匹配,主机名后面不能带任何的字符串
}
# 2. 通用匹配
location /XXX {
  # 匹配所有以/xxx开头的路径
}
# 3. 正则匹配
location ~ /XXX {
  # 匹配所有以/XXX开头的路径
}
# 4. 匹配开头路径
location ^~ /images/ {
  # 匹配所有以/images开头的路径
}
# 5. ~* \.(gif|jpg|png)$ {
  # 匹配以gif或者png为结尾的路径
}

开启gzip压缩功能

目的:提高传输效率,节约带宽
方法:在核心配置文件nginx.con配置一下代码

#开启gzip压缩功能
gzip on
#限制最小压缩,小于1字节文件不会压缩
gzip_min_length 1;
#定义压缩的级别(压缩比,文件越大,压缩越多,但是cpu使用会越多)
gzip_comp_level 3;#数字1-9自定义
#定义压缩文件的类型
gzip_types text/plain.....;

跨域支持

#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;

nginx防盗链配置支持

#对源站点验证
valid_referers none www.xiaodunan.com;
#非法引入会进入下方判断
if ($invalid_referer)
    {
     return 404;
     }

img

posted @ 2023-04-22 15:04  小肚腩吖  阅读(17)  评论(0编辑  收藏  举报