• 【nginx】Nginx配置:



    一、什么是Nginx:

    在这里插入图片描述

    二、为什么使用Nginx:

    在这里插入图片描述

    三、如何处理请求:

    在这里插入图片描述

    四、什么是正向代理和反向代理:

    在这里插入图片描述
    在这里插入图片描述

    五、nginx 启动和关闭:
    进入目录:/usr/local/nginx/sbin
    启动命令:./nginx
    重启命令:nginx -s reload
    快速关闭命令:./nginx -s stop
    有序地停止,需要进程完成当前工作后再停止:./nginx -s quit
    直接杀死nginx进程:killall nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    六、目录结构:

    在这里插入图片描述

    七、配置文件nginx.conf:
    # 启动进程,通常设置成和cpu的数量相等
    worker_processes  1;
    
    # 全局错误日志定义类型,[debug | info | notice | warn | error | crit]
    error_log  logs/error.log;
    error_log  logs/error.log  notice;
    error_log  logs/error.log  info;
    
    # 进程pid文件
    pid        /var/run/nginx.pid;
    
    # 工作模式及连接数上限
    events {
        # 仅用于linux2.6以上内核,可以大大提高nginx的性能
        use   epoll;
    
        # 单个后台worker process进程的最大并发链接数
        worker_connections  1024;
    
        # 客户端请求头部的缓冲区大小
        client_header_buffer_size 4k;
    
        # keepalive 超时时间
        keepalive_timeout 60;
    
        # 告诉nginx收到一个新连接通知后接受尽可能多的连接
        # multi_accept on;
    }
    
    # 设定http服务器,利用它的反向代理功能提供负载均衡支持
    http {
        # 文件扩展名与文件类型映射表义
        include       /etc/nginx/mime.types;
    
        # 默认文件类型
        default_type  application/octet-stream;
    
        # 默认编码
        charset utf-8;
    
        # 服务器名字的hash表大小
        server_names_hash_bucket_size 128;
    
        # 客户端请求头部的缓冲区大小
        client_header_buffer_size 32k;
    
        # 客户请求头缓冲大小
        large_client_header_buffers 4 64k;
    
        # 设定通过nginx上传文件的大小
        client_max_body_size 8m;
    
        # 开启目录列表访问,合适下载服务器,默认关闭。
        autoindex on;
    
        # sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
        # 必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度
        sendfile        on;
    
        # 此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
        #tcp_nopush     on;
    
        # 连接超时时间(单秒为秒)
        keepalive_timeout  65;
    
    
        # gzip模块设置
        gzip on;               #开启gzip压缩输出
        gzip_min_length 1k;    #最小压缩文件大小
        gzip_buffers 4 16k;    #压缩缓冲区
        gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
        gzip_comp_level 2;     #压缩等级
        gzip_types text/plain application/x-javascript text/css application/xml;
        gzip_vary on;
    
        # 开启限制IP连接数的时候需要使用
        #limit_zone crawler $binary_remote_addr 10m;
    
        # 指定虚拟主机的配置文件,方便管理
        include /etc/nginx/conf.d/*.conf;
    
    
        # 负载均衡配置
        upstream aaa {
            # 请见上文中的五种配置
        }
    
    
       # 虚拟主机的配置
        server {
    
            # 监听端口
            listen 80;
    
            # 域名可以有多个,用空格隔开
            server_name www.aaa.com aaa.com;
    
            # 默认入口文件名称
            index index.html index.htm index.php;
            root /data/www/sk;
    
            # 图片缓存时间设置
            location ~ .*.(gif|jpg|jpeg|png|bmp|swf)${
                expires 10d;
            }
    
            #JS和CSS缓存时间设置
            location ~ .*.(js|css)?${
                expires 1h;
            }
    
            # 日志格式设定
            #$remote_addr与 $http_x_forwarded_for用以记录客户端的ip地址;
            #$remote_user:用来记录客户端用户名称;
            #$time_local:用来记录访问时间与时区;
            #$request:用来记录请求的url与http协议;
            #$status:用来记录请求状态;成功是200,
            #$body_bytes_sent :记录发送给客户端文件主体内容大小;
            #$http_referer:用来记录从那个页面链接访问过来的;
            log_format access '$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" $http_x_forwarded_for';
    
            # 定义本虚拟主机的访问日志
            access_log  /usr/local/nginx/logs/host.access.log  main;
            access_log  /usr/local/nginx/logs/host.access.404.log  log404;
    
            # 对具体路由进行反向代理
            location /connect-controller {
    
                proxy_pass http://127.0.0.1:88;
                proxy_redirect off;
                proxy_set_header X-Real-IP $remote_addr;
    
                # 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $host;
    
                # 允许客户端请求的最大单文件字节数
                client_max_body_size 10m;
    
                # 缓冲区代理缓冲用户端请求的最大字节数,
                client_body_buffer_size 128k;
    
                # 表示使nginx阻止HTTP应答代码为400或者更高的应答。
                proxy_intercept_errors on;
    
                # nginx跟后端服务器连接超时时间(代理连接超时)
                proxy_connect_timeout 90;
    
                # 后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据
                proxy_send_timeout 90;
    
                # 连接成功后,后端服务器响应的超时时间
                proxy_read_timeout 90;
    
                # 设置代理服务器(nginx)保存用户头信息的缓冲区大小
                proxy_buffer_size 4k;
    
                # 设置用于读取应答的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k
                proxy_buffers 4 32k;
    
                # 高负荷下缓冲大小(proxy_buffers*2)
                proxy_busy_buffers_size 64k;
    
                # 设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长
                # 设定缓存文件夹大小,大于这个值,将从upstream服务器传
                proxy_temp_file_write_size 64k;
            }
    
            # 动静分离反向代理配置(多路由指向不同的服务端或界面)
            location ~ .(jsp|jspx|do)?$ {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_pass http://127.0.0.1:8080;
            }
        }
    }
    
    • 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
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    八、location:

    在这里插入图片描述
    在这里插入图片描述

    九、单页面应用刷新404问题:
    location / {
      try_files $uri $uri/ /index.html;
    }
    
    • 1
    • 2
    • 3
    十、配置跨域请求:
    server {
        listen   80;
        location / {
            # 服务器默认是不被允许跨域的。
            # 配置`*`后,表示服务器可以接受所有的请求源(Origin),即接受所有跨域的请求
            add_header Access-Control-Allow-Origin *;
            
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
            
            # 发送"预检请求"时,需要用到方法 OPTIONS ,所以服务器需要允许该方法
            # 给OPTIONS 添加 204的返回,是为了处理在发送POST请求时Nginx依然拒绝访问的错误
            if ($request_method = 'OPTIONS') {
                return 204;
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    十一、开启gzip压缩:
    # gzip模块设置
    gzip on;               #开启gzip压缩输出
    gzip_min_length 1k;    #最小压缩文件大小
    gzip_buffers 4 16k;    #压缩缓冲区
    gzip_http_version 1.0; #压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
    gzip_comp_level 2;     #压缩等级
        
    # 设置什么类型的文件需要压缩
    gzip_types text/plain application/x-javascript text/css application/xml;
        
    # 用于设置使用Gzip进行压缩发送是否携带“Vary:Accept-Encoding”头域的响应头部
    # 主要是告诉接收方,所发送的数据经过了Gzip压缩处理
    gzip_vary on;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    十二、配置https:

    【Nginx实现本地http转https请求】https://blog.csdn.net/m0_53151031/article/details/125497917

    十三、http跳转https:

    【return和rewrite的区别】https://www.nginx.com/blog/creating-nginx-rewrite-rules/
    Nginx配置http跳转https

    【1】return 301

    #这是 Nginx 新版本的写法,推荐使用。在 Nginx 80 监听服务上加一行:
    return 301 https://$server_name$request_uri; #http跳转https
    
    • 1
    • 2
    #完整配置如下:
    #管理端
    server {
        listen       10003;
        server_name localhost;
        try_files $uri $uri/ /index.html;
        root    /home/source/shop/manager/dist;
    }
    #管理端转发
    server {
        listen       80;
        server_name admin-xxxxx.xxx.xxx;
        return 301 https://$server_name$request_uri; #http跳转https
    }
    #管理端https
    server {
         listen 443 ssl;
         server_name admin-xxxxx.xxx.xxx;
         ssl_certificate ../cert/server.crt;
         ssl_certificate_key ../cert/server.key;
         ssl_session_timeout 5m;
         ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
         ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
         ssl_prefer_server_ciphers on;
    
         location / {
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header Host $http_host;
             proxy_pass http://localhost:10003;
         }
    }
    
    • 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

    【2】rewrite

    #Nginx 老版本的写法,不推荐使用。将 “#http跳转https” 这行修改为:
    rewrite ^(.*)$  https://admin-xxxxx.xxx.xxx permanent;
    或
    rewrite ^(.*)$  https://$host$1 permanent;
    
    • 1
    • 2
    • 3
    • 4

    【3】497 状态码

    #将 “#http跳转https” 这行修改为:
    error_page 497  https://$host$uri?$args;
    
    • 1
    • 2

    【4】meta 刷新

    #(1)写一个 index.html,内容是 http 向 https 跳转:
    index.html
    <html>
      <meta http-equiv=”refresh” content=”0; url=https://admin-xxxxx.xxx.xxx/”>
    </html>
    #(2)再修改 Nginx 配置文件,将 “#http跳转https” 这行修改为:
    error_page 404 https://admin-xxxxx.xxx.xxx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    一句话代码富集分析gost ghost
    C语言学习之路(基础篇)—— 文件操作(下)
    【IEEE】IEEE论文从投稿到发表全流程案例说明
    用百度云怎么重装电脑系统
    基于springboot实现智慧外贸平台系统【项目源码+论文说明】计算机毕业设计
    python编程:创建 SQLite 数据库和表的图形用户界面应用程序
    mysql数据库数据如何迁移目录
    颜色直方图图像检索
    运行栏在弹窗上面的时候才能控制弹窗,怎么解决
    【方案分享】抖音运营攻略:平台规则、爆款涨粉、运营技巧、内容变现.pdf(附下载链接)...
  • 原文地址:https://blog.csdn.net/weixin_53791978/article/details/133523459