广告位联系
返回顶部
分享到

nginx:stable镜像的使用及说明

nginx 来源:互联网 作者:佚名 发布时间:2026-06-19 09:19:02 人浏览
摘要

nginx:stable镜像的使用 以前使用的nginx:stable-alpine 但是https加载很慢,所以尝试换成nginx:stable,结果不仅https 快了,页面加载和接口调用都快了 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 dock

nginx:stable镜像的使用

以前使用的nginx:stable-alpine

但是https加载很慢,所以尝试换成 nginx:stable,结果不仅https 快了,页面加载和接口调用都快了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

docker run -d \

    --name nginx-stable \

    --restart unless-stopped \

    --network app \

    --hostname nginx \

    -p 80:80 \

    -p 443:443 \

    -e TZ=Asia/Shanghai \

    -e NGINX_ENTRYPOINT_QUIET_LOGS=1 \

    -v /opt/docker-data/nginx/log:/etc/nginx/logs \

    -v /opt/docker-data/nginx/static:/usr/share/nginx/html:ro \

    -v /opt/docker-data/nginx/https-cert:/etc/nginx/https-cert:ro \

    -v /opt/docker-data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro \

    -v /opt/docker-data/nginx/conf/conf.d:/etc/nginx/conf.d:ro \

    -v /opt/docker-data/nginx/conf/sites-available:/etc/nginx/sites-available:ro \

    -v /opt/docker-data/nginx/conf/sites-enabled:/etc/nginx/sites-enabled:ro \

    -v /opt/docker-data/nginx/cache:/var/cache/nginx \

    --ulimit nofile=65536:65536 \

    --sysctl net.core.somaxconn=65535 \

    --cpus 2 \

    --memory 1g \

    --memory-swap 1g \

    nginx:stable

1

docker logs -f -t --tail 50 nginx-stable

在宿主机 /opt/docker-data/nginx/ 下新建一下文件夹 

1

static sites-enabled sites-available log https-cert conf cache

在 conf 文件夹下新建 nginx.conf文件 和 conf.d 文件夹

nginx.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

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

#user  nobody;

# worker 数和服务器的 cpu 数相等是最为适宜的。

# 设少了会浪费 cpu,设多了会造成 cpu 频繁切换上下文带来的损耗。

worker_processes  2;

  

# work 绑定 cpu(4 work 绑定 4cpu)。

# worker_cpu_affinity 0001 0010 0100 1000

# work 绑定 cpu (4 work 绑定 8cpu 中的 4 个) 。

# worker_cpu_affinity 0000001 00000010 00000100 00001000

  

  

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

error_log  /etc/nginx/logs/error.log warn;

  

pid /tmp/nginx.pid;

  

  

events {

    # 普通的静态访问最大并发数建议:worker_connections * worker_processes / 2

    # 作为反向代理来说,最大并发数量建议 worker_connections * worker_processes / 4

    # 因为作为反向代理服务器,每个并发会建立与客户端的连接和与后端服务的连接,会占用两个连接。

    worker_connections  1024;

}

  

  

http {

    server_tokens off;

    include       mime.types;

    default_type  application/octet-stream;

  

    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;

    access_log /etc/nginx/logs/access.log;

  

    sendfile        on;

    #tcp_nopush     on;

  

    #keepalive_timeout  0;

    keepalive_timeout  65;

  

    gzip on;

  

    # 关闭etag,比较消耗性能,仅使用Last-Modified

    etag off;

  

    # 设置允许压缩的页面最小字节数; 这里表示如果文件小于这个大小,就不用压缩,因为没有意义,本来就很小.

    gzip_min_length 2k;

  

    # 设置压缩比率,最小为1,处理速度快,传输速度慢;9为最大压缩比,处理速度慢,传输速度快;

    # 这里表示压缩级别,可以是0到9中的任一个,级别越高,压缩就越小,节省了带宽资源,但同时也消耗CPU资源,所以一般折中为6

    gzip_comp_level 6;

    # 指定压缩的文件类型

    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;

  

    limit_conn_log_level error;

    limit_conn_status 429;

    limit_req_status 429;

  

    # 设置了名为 ip_conn_pool 的存储区,大小为20兆字节,根据IP地址

    limit_conn_zone $binary_remote_addr zone=ip_conn_pool:32m;

    # 设置了名为 per_server_pool 的存储区,大小为20兆字节,根据server

    limit_conn_zone $server_name zone=per_server_pool:32m;

    limit_req_zone $binary_remote_addr zone=api_limit:256m rate=20r/s;

    # 其中$binary_remote_addr有时需要根据自己已有的log_format变量配置进行替换

  

    #server {

    #  listen 80 default_server;

    #  listen 443 default_server;

    #  server_name _;

    #  ssl_reject_handshake on;

    #  return 444;

    #}

  

    client_max_body_size 50m;

  

    include /etc/nginx/conf.d/*.conf;

}

在conf.d 文件夹下新建jszj.conf

jszj.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

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

server {

    listen 80;

    #listen 8001;

    listen 443 ssl;

    http2 on;

     

    ssl_certificate /etc/nginx/https-cert/jnajszj.mmwzcloud.com.pem;

    ssl_certificate_key /etc/nginx/https-cert/jnajszj.mmwzcloud.com.key;

  

    server_name jszj.mmwzcloud.com default_server;

  

    # 1. 优化 SSL 配置

    ssl_protocols TLSv1.2 TLSv1.3;

    ssl_ciphers 'TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';

    ssl_prefer_server_ciphers off;

     

    # 2. 优化会话缓存

    ssl_session_cache shared:SSL:10m;  # 减小到 10m

    ssl_session_timeout 1h;

    ssl_session_tickets on;

     

    # 3. 禁用有问题的功能

    ssl_stapling off;

    ssl_stapling_verify off;

     

    # 4. 关键优化:减小缓冲区

    ssl_buffer_size 4k;  # 从 16k 改为 4k

     

    # 防止 MIME 类型混淆攻击

    add_header X-Content-Type-Options nosniff;

  

  

    proxy_set_header X-Real-IP $remote_addr;

    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_set_header Host $host;

    proxy_redirect off;

  

  

    location /api/ {

        proxy_hide_header Access-Control-Allow-Origin;

        add_header Access-Control-Allow-Origin *;

        add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";

        add_header Access-Control-Allow-Methods GET,POST,OPTIONS,HEAD,PUT,DELETE;

        add_header Access-Control-Allow-Credentials false; 

        if ($request_method = OPTIONS) {

            return 204;

        }

  

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-Proto $scheme;  # 重要!告诉后端是 HTTPS

         

        proxy_pass http://jszj:38080/;

        #proxy_pass http://www.baidu.com/;

    }

 

    #h5项目  

    location /apph/ {

        alias   /usr/share/nginx/html/apph/;

        # VUE History 模式下刷新网页404问题

        try_files $uri $uri/ /apph/index.html;

        index  index.html;

        #expires 12h;

        add_header Cache-Control "no-cache,must-revalidate";

        error_log /etc/nginx/logs/error.log;

        access_log /etc/nginx/logs/access.log;

    }

 

    #h5项目  

    location /static-file/ {

        alias   /usr/share/nginx/html/static-file/;

        # VUE History 模式下刷新网页404问题

        #try_files $uri $uri/ /apph/index.html;

        #index  index.html;

        #expires 12h;

        add_header Cache-Control "no-cache,must-revalidate";

        error_log /etc/nginx/logs/error.log;

        access_log /etc/nginx/logs/access.log;

    }

 

     

 

    #后台管理

    location / {

      # 项目在 /usr/share/nginx/html/vue-admin 目录

       root /usr/share/nginx/html/vue-admin;

       index index.html;

     

       # 修正 try_files,避免循环

       try_files $uri $uri/ /index.html;

     

       add_header Cache-Control "no-cache,must-revalidate";

       error_log /etc/nginx/logs/error.log;

       access_log /etc/nginx/logs/access.log;

    }

  

    error_page 429 /429;

    location = /429 {

        default_type application/json;

        return 429 '{"code":429,"message":"当前访问人数过多, 请稍后再试"}';

    }

     

    error_page 502 /server_shutdown;

    error_page 503 /server_shutdown;

    location = /server_shutdown {

        default_type application/json;

        return 502 '{"code":502,"message":"服务器升级维护中, 请稍后再试"}';

    }

}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • nginx:stable镜像的使用及说明
    nginx:stable镜像的使用 以前使用的nginx:stable-alpine 但是https加载很慢,所以尝试换成nginx:stable,结果不仅https 快了,页面加载和接口调用都快了
  • Nginx使用upstream后端接口报 400
    upstream模块介绍 Nginx的负载均衡功能依赖于ngx_http_upsteam_module模块,所支持的代理方式包括proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, memcached_pass和
  • 查看nginx是否已经启动的实现方式
    在 Ubuntu 或其他 Linux 系统上,要查看 Nginx 是否已经启动,您可以使用以下几种方法之一: 方法一:使用systemctl命令 Nginx 通常作为 systemd 服
  • 在Ubuntu上安装Nginx的实现过程
    在Ubuntu系统中从源码安装Nginx可以让您自定义Nginx的编译选项和模块,以满足特定需求。 以下是详细的步骤指南: 前提条件 更新系统包列表
  • Windows上启动停止Nginx服务器
    在 Windows 上开发 Django、Vue 或其他 Web 项目时,Nginx 往往是我们最常用的反向代理服务器。然而,不同于 Linux 系统上顺手的 systemctl 命令,
  • 使用nginx实现ssh跳板机方式
    基础环境 跳板机,IP:192.168.3.174 控制机01,IP:192.168.2.78 控制机02,IP:192.168.2.79 控制机01、控制机02只允许跳板机访问。 nginx安装 这里使用
  • Nginx缓存清理实现方式
    Nginx 作为一个高效的 Web 服务器和反向代理服务器,在提供快速的页面响应和优化 Web 性能方面起着至关重要的作用。 Nginx 的缓存机制通过存
  • 清空nginx缓存并强制刷新实现过程
    清空nginx缓存并强制刷新 当对nginx的文件进行修改或更新时,可能会出现旧文件被缓存而无法立即生效的问题,此时需要清空nginx的文件缓存
  • Nginx静态资源优化、压缩、缓存介绍

    Nginx静态资源优化、压缩、缓存介绍
    优化 Nginx对静态资源如何进行优化配置。这里从三个属性配置进行优化: 1 2 3 sendfile on; tcp_nopush on; tcp_nodeplay on; send?le 用来开启高效的文件
  • Nginx HTTP反向代理负载均衡实验教程

    Nginx HTTP反向代理负载均衡实验教程
    一、实验目标 在 192.168.65.135 上部署 Nginx,作为七层 HTTP 反向代理。 将www.xiaotiantian.org的流量轮询转发到两台后端 Web: 192.168.65.131:80 192.16
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计