学习笔记 学习笔记
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)

爱做梦的奋斗青年

曾梦想仗剑走天涯,后来bug太多就没去
🐱 首页
    • 🚅 JAVA
    • 🚆 Python
    • 🧭 VUE
    • 🌐 JavaScript
    • 🗺 CSS
  • 🎃 MySQL
  • 🛶 Redis
  • 🛳 Nginx
  • ⚽ Dokcer
  • 🏓 Elasticsearch
  • 🏙 Windows
  • 🗽 Centos
  • ⚓ Gitlab
  • 🙈 分类
  • 🙉 标签
  • 🙊 归档
    • 👣 随笔
    • 🌹 关于
GitHub (opens new window)
  • spring-boot

  • mysql

  • 达梦数据库

  • Redis

  • nginx

    • Nginx安装
    • Nginx核心配置
    • Nginx实战配置
      • 配置文件
      • nginx命令
      • 每天定时切割Nginx日志的脚本
      • Nginx加入systemctl进行管理服务
    • openrest的安装
  • Docker

  • Elasticsearch

  • oss

  • 应用框架
  • nginx
爱做梦的奋斗青年
2021-02-02
目录

Nginx实战配置

官方配置:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream (opens new window)

# 配置文件

demo.conf
mkdir -p /usr/local/nginx/conf.d && cat >> /usr/local/nginx/conf.d/demo.conf << EOF
# 轮询+权重
upstream api01 {
    server 192.16.18.101:80 weight=1 max_fails=10 fail_timeout=120s;
    server 192.16.18.102:80 weight=2 max_fails=10 fail_timeout=120s;
    server 192.16.18.103:80 backup;
    server 192.16.18.104:80 down;
}
# 基于客户端IP的分配方式
upstream api02 {
    ip_hash;
    server 192.16.18.101:8080 weight=1 max_fails=10 fail_timeout=120s;
    server 192.16.18.102:8080 weight=1 max_fails=10 fail_timeout=120s;
}

server {
    listen 80;
    server_name  dev.demo.com;                  # 可以通过域名访问
    server_name  www.demo.com;
    server_name  localhost;
    server_name  _;                             # 默认服务,允许使用ip访问

    client_max_body_size        10M;            # 允许客户端请求的单文件最大大小,单位默认字节。
    client_body_buffer_size     128k;           # 为客户端请求设置的缓存大小。

    proxy_connect_timeout       60s;            # 后端服务器连接超时时间。默认 60 秒
    proxy_send_timeout          90;             # 后端服务器数据回传时间(代理发送超时)
    proxy_read_timeout          60s;            # 发出请求后等待后端服务器响应的最长时限。默认 60 秒
    proxy_buffer_size           4k;             # 设置代理服务器(nginx)保存用户头信息的缓冲区大小
    proxy_buffers               4 32k;          # proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
    proxy_busy_buffers_size     64k;            # 高负荷下缓冲大小(默认proxy_buffers*2)
    proxy_temp_file_write_size  64k;            # 设定缓存文件夹大小,大于这个值,将从upstream服务器传
    proxy_buffering             off;            # 开启从后端被代理服务器的响应内容缓冲区。默认值 on


    

    gzip                        on;             # 开启gzip
    gzip_min_length             1m;             # 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
    gzip_buffers                4 16k;          # 设置压缩所需要的缓冲区大小
    gzip_http_version           1.0;            # 设置gzip压缩针对的HTTP协议版本,不需要改
    gzip_comp_level             6;              # gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
    gzip_types                  text/plain application/javascript application/xml application/cc-directory application/json;  # 进行压缩的文件类型
    gzip_vary                   on;             # 是否在http header中添加Vary: Accept-Encoding,建议开启
    gzip_disable                "MSIE [1-6]\."; # 禁用IE 6 gzip

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    root /usr/local/nginx/html;
    index index.html index.htm;

    # 静态代理
    location / {
    }
    # 扩展名拦截
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
        root    /opt/demo/www/;
        # 过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
        expires 30d;
    }
    # 目录名拦截
    location ~ .*(css|js|images|html).+ {
        root    /opt/demo/www/;
    }
    
    # 正则匹配 动态反向代理
    location  ~* ^/api/01/ {
        proxy_pass  http://api01;
    }

    # 查看nginx状态
    location = /status {
        stub_status on;
        access_log on;
    }
    # 下载站点
    location ^~ /download {
        alias html;                             # 展示的目录 
        index _;                                # 去掉默认的界面
        # 如果不做这个配置,点击目录下的txt文件,大部分浏览器默认是直接浏览的。这里通过添加响应头来控制。
        if ($request_filename ~* ^.*?\.(html|txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|conf)$){
                add_header Content-Disposition 'attachment;';
        }
        ## nginx默认的索引目录
        autoindex              on;              # 开启整个目录浏览下载
        autoindex_localtime    on;              # 显示文件的GMT时间
        autoindex_exact_size   off;             # 显示文件的大小

        ## 如果安装了 fancyindex 美好索引目录
        # fancyindex on;
        # fancyindex_localtime on;
        # fancyindex_exact_size off;
        # fancyindex_name_length 255;
        ## fancyindex_header "/fancyindex/header.html";  ## 可选
        ## fancyindex_footer "/fancyindex/footer.html";  ## 可选
        ## fancyindex_ignore "examplefile.html";         ## 可选
        ## fancyindex_ignore "fancyindex";               ## 可选 忽略显示
    }

    # 异常界面
    error_page   404              /404.html;
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
    # 禁止访问.htxxx文件
    location ~ /\.ht {
        deny all;
    }
}
server {
    listen       8888;#默认端口是80,如果端口没被占用可以不用修改
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;
    root        /opt/blog/dist;             #vue项目的打包后的dist

    location / {
        try_files $uri $uri/ @router;        #需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
        index  index.html index.htm;
    }
    #对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
    #因此需要rewrite到index.html中,然后交给路由在处理请求资源
    location @router {
        rewrite ^.*$ /index.html last;
    }
    #.......其他部分省略
}

EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 配置运行用户或者组,默认为nobody nobody。
#user  nobody;
# worker进程数,通常等于CPU数量或者CPU的倍数。
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

# 进程可以打开的最大文件数量
worker_rlimit_nofile  1024;

events {
    # 事件驱动模型,多路复用IO
    use epoll;
    # worker线程的最大并发链接数
    worker_connections  1024;
}


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

    # 定义日志格式 main
    #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;

    include /usr/local/nginx/conf.d/*.conf;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# nginx命令

# 查看Nginx主进程号
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'
cat /usr/local/nginx/logs/nginx.pid
1
2
3

# 每天定时切割Nginx日志的脚本

# 编写脚本

cat >> /usr/local/nginx/sbin/cut_nginx_log.sh << EOF
#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`
EOF
1
2
3
4
5
6
7
8
9
10
11

# 设置crontab,每天凌晨00:00切割nginx访问日志

crontab -e
#  输入一下面定时任务
00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh
1
2
3

# Nginx加入systemctl进行管理服务

cat >> /usr/lib/systemd/system/nginx.service << EOF
[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/local/nginx/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /usr/local/nginx/logs/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload && systemctl enable nginx && systemctl start nginx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
编辑此页 (opens new window)
#nginx
上次更新: 2022/02/24 09:06:41
Nginx核心配置
openrest的安装

← Nginx核心配置 openrest的安装→

Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式