nginx 常用命令 以及配置文件

192 阅读5分钟

mac电脑使用brewhome安装

brew install nginx
brew info nginx
 nginx #启动服务
 nginx -s stop #停止服务(直接走)
 nginx -s reload  #重新加载
 nginx -s reopen #重新启动
 nginx -s quit #退出(处理完事情走)
 nginx -t 启动测试
open /opt/homebrew/etc/nginx/ #查看nginx安装目录

-s : signal (信号)

  • nginx安装位置:
/opt/homebrew/Cellar/nginx/1.21.1
  • 配置文件路径:
/opt/homebrew/etc/nginx

linux/mac文件目录

  • opt: (option) 目录通常用于存放可选的或者额外安装的软件包。这些软件包通常不是系统默认提供的,而是由用户或第三方提供的。

  • etc: (et cetera) 这个目录包含了许多不同软件和系统组件的配置文件,用来配置系统的各种行为。


配置文件


#user  nobody; # 指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行
worker_processes  1;  # 指定工作进程的个数,默认是1个。具体可以根据服务器cpu数量进行设置, 比如cpu有4个,可以设置为4。如果不知道cpu的数量,可以设置为auto。 nginx会自动判断服务器的cpu个数,并设置相应的进程数
#error_log  logs/error.log;  # 用来定义全局错误日志文件输出路径,这个设置也可以放入http块,server块,日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info; # 指定error日志位置和日志级别
#pid        logs/nginx.pid;  # 用来指定进程pid的存储文件位置

events {
    accept_mutex on;   # 设置网路连接序列化,防止惊群现象发生,默认为on
    use epoll; # Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll,其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选
    worker_connections  1024; # 用于定义Nginx每个工作进程的最大连接数,默认是1024。最大客户端连接数由worker_processes和worker_connections决定,即Max_client=worker_processes*worker_connections在作为反向代理时,max_clients变为:max_clients = worker_processes *worker_connections/4。进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效
}

# 对HTTP服务器相关属性的配置如下
http {
    include       mime.types; # 引入文件类型映射文件 
    default_type  application/octet-stream; # 如果没有找到指定的文件类型映射 使用默认配置 
    # 设置日志打印格式 日志名称为main
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    # 关联(log_format)名称main的日志格式并打印出来
    #access_log  logs/access.log  main; # 设置日志输出路径以及 日志级别
    sendfile        on; # 开启零拷贝 省去了内核到用户态的两次copy故在文件传输时性能会有很大提升
    #tcp_nopush     on; # 数据包会累计到一定大小之后才会发送,减小了额外开销,提高网络效率
    keepalive_timeout  65; # 设置nginx服务器与客户端会话的超时时间。超过这个时间之后服务器会关闭该连接,客户端再次发起请求,则需要再次进行三次握手。
    #gzip  on; # 开启压缩功能,减少文件传输大小,节省带宽。
    sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
    
    # 配置你的上游服务(即被nginx代理的后端服务)的ip和端口/域名
    upstream backend_server { 
        server 172.30.128.65:8080 weight=1; #设置权重比例
        server 172.30.128.65:8081 weight=1 backup; #备机
    }

    server {
        listen       80; #nginx服务器监听的端口
        server_name  localhost; #监听的地址 nginx服务器域名/ip 多个使用英文逗号分割
        #access_log  logs/host.access.log  main; # 设置日志输出路径以及 级别,会覆盖http指令块的access_log配置
        
        # location用于定义请求匹配规则。 以下是实际使用中常见的3中配置(即分为:首页,静态,动态三种)
       
        # 第一种:直接匹配网站根目录,通过域名访问网站首页比较频繁,使用这个会加速处理,一般这个规则配成网站首页,假设此时我们的网站首页文件就是: usr/local/nginx/html/index.html
        location = / {  
            root   html; # 静态资源文件的根目录 比如我的是 /usr/local/nginx/html/
            index  index.html index.htm; # 静态资源文件名称 比如:网站首页html文件
        }
        # 第二种:静态资源匹配(静态文件修改少访问频繁,可以直接放到nginx或者统一放到文件服务器,减少后端服务的压力),假设把静态文件我们这里放到了 usr/local/nginx/webroot/static/目录下
        location ^~ /static/ {
            alias /webroot/static/; 访问 ip:80/static/xxx.jpg后,将会去获取/url/local/nginx/webroot/static/xxx.jpg 文件并响应
        }
        # 第二种的另外一种方式:拦截所有 后缀名是gif,jpg,jpeg,png,css.js,ico这些 类静态的的请求,让他们都去直接访问静态文件目录即可
        location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
            root /webroot/static/;
        }
        # 第三种:用来拦截非首页、非静态资源的动态数据请求,并转发到后端应用服务器 
        location / {
            proxy_pass http://backend_server; #请求转向 upstream是backend_server 指令块所定义的服务器列表
            deny 192.168.3.29; #拒绝的ip (黑名单)
            allow 192.168.5.10; #允许的ip(白名单)
        }
        
        # 定义错误返回的页面,凡是状态码是 500 502 503 504 总之50开头的都会返回这个 根目录下html文件夹下的50x.html文件内容
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }
    # 其余的server配置 ,如果有需要的话
    #server {
        ......
    #    location / {
               ....
    #    }
    #}
    
    # include /etc/nginx/conf.d/*.conf;  # 一般我们实际使用中有很多配置,通常的做法并不是将其直接写到nginx.conf文件,
    # 而是写到新文件 然后使用include指令 将其引入到nginx.conf即可,这样使得主配置nginx.conf文件更加清晰。
    
}


测试user指令

  • user:用于配置运行Nginx服务器的worker讲程的用户和用户组。
  1. 设置nginx用户信息user Even如下
user Even; 
#error_log  logs/error.log;  
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info; 
  1. linux创建用户
useradd Even
  1. 创建/root/html/index.html页面,添加内容如下
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>/root/html/index.html</title>
</head>
<body>
  <div>
  /root/html/index.html
  </div>
</body>
</html>
  1. 修改nginx.conf配置
location / {
    root /root/html;
    index index.html index.htm;
}
  1. 测试启动访问

页面会报403拒绝访问的错误

  1. 分析原因

因为当前用户没有访问/root/html目录的权限

  1. 将文件创建到 /home/Even/html/index.html, 修改配置
location / {
    root /root/Even/html;
    index index.html index.htm;
}
  1. 再次测试启动访问

正常访问


权重upstream

语法: upstream + 名称

# kuangstudy名称(可以随便定义)
upstream kuangstudy {
  server 127.0.0.1:8080 weight=1; // 权重1
  server 127.0.0.1:8081 weight=2; // 权重2
}

server {
  listen   80;
  server_name localhost;
  #charset koi8-r;
  #access
  location / {
    root html;
    index index.html index.htm;
    proxy_pass http://kuangstudy;
  }
}

default_type 数据类型

  • application/octet-stream 二进制数据类型
  • text/html html类型
  • application/json json类型
# 返回html并展示
location /get_text {
  default_type text/html
  return 200 "this is nginx's text"
}

# 返回json数据
location /get_json {
  default_type application/json
  return 200 '{"name":"TOM","age": 12}'
}

服务配置

拉取镜像nginx

docker pull nginx:latest

docker-compose.yml文件

version: '2.4'
services:
  app:
    image: "nginx:latest"
    container_name: web-pingmesh
    restart: always
    network_mode: host
    cpus: 2
    mem_limit: 2g
    volumes:
      - ./nginx-app.conf:/etc/nginx/conf.d/nginx-app.conf
      - ./dist:/web
    ports:
      - 80:80

nginx-app.conf文件

upstream api {
    least_conn;
    server 127.0.0.1:8000;
}

server {
    client_max_body_size 100m;
    listen       80 default_server;
#    listen       [::]:80 default_server;
    index        index.html;
    server_name  _;

   location / {
        root       /web;
        try_files $uri $uri/ /index.html;

        if ($request_uri ~* /custom.*\-proxy ) {
            rewrite (/custom.*\-proxy)(/.*) /custom-proxy$2;
        }

        if ($request_filename ~ .*\.html$) {
            add_header Cache-Control no-cache;
        }
    }

    location /cn-api/ {
        proxy_pass http://10.200.20.183;
        proxy_set_header x-forwarded-for $remote_addr;
        proxy_connect_timeout 60;
        proxy_read_timeout 300;
        break;
    }
    #include /etc/nginx/conf.d/app/*.conf;
}