Linux安装部署服务:Nginx和Openresty

Linux安装部署服务:Nginx和Openresty

    • 一、安装环境说明
      • 1.1 虚拟机环境
      • 1.2 nginx 安装包
      • 1.3 openresty 安装包
    • 二、安装 nginx 服务
      • 2.1 安装前环境准备
      • 2.2 源码安装 nginx
      • 2.3 开机自启 nginx
    • 三、安装 openresty 服务
      • 3.1 安装前环境准备
      • 3.2 源码安装 openresty
      • 3.4 离线安装 openresty
      • 3.5 开机自启 openresty
    • 四、nginx 常用配置
      • 4.1 nginx 基础配置
      • 4.2 nginx 重定向
      • 4.3 nginx 跨域访问
      • 4.4 nginx 防盗链
      • 4.5 nginx 证书配置
      • 4.6 nginx 动静分离
      • 4.7 nginx 资源压缩
      • 4.8 nginx 负载均衡
      • 4.9 nginx 代理访问
      • 5.0 nginx 配置简介
    • 五、安装配置问题
      • 5.1 问题 1:编译报错
      • 5.2 问题 2:配置报错


写在前面:

  1. 本文在部分内容方面,参考了CSDN部分博主的文章内容,如有冒犯请联系博主协商处理。
  2. 本文所有安装部分的内容和部分配置的内容都经博主实测有效,如有错误的地方,欢迎大家指正学习。
  3. 文章创作不易,请各位看官给个三连,博主在此先行感谢了!!!

一、安装环境说明

1.1 虚拟机环境

  • 虚拟机或服务器环境

    操作系统IP 地址系统版本
    CentOS 7.9192.168.8.100CentOS Linux release 7.9.2009 (Core)

1.2 nginx 安装包

  • 官网地址:nginx: download

  • 推荐使用 nginx-1.24.0版本,该版本被广泛使用

1.3 openresty 安装包

  • 基本介绍:OpenResty 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。

  • 官网地址:OpenResty® - 开源官方站

  • 此处推荐两个版本的安装包 openresty-1.21.4.4openresty-1.25.3.1版本

二、安装 nginx 服务

2.1 安装前环境准备

  • 安装 nginx 编译所需相关依赖

    $ yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
    

2.2 源码安装 nginx

  • 上传 nginx 压缩包,并解压

    $ tar -zxvf nginx-1.24.0.tar.gz
    $ ll /root
    -rw-r--r--  1 root  root    1112471 95 23:54 nginx-1.24.0.tar.gz
    drwxr-xr-x  8 root  root        158 411 2023 nginx-1.24.0
    
  • 进入安装目录,编译安装 nginx

    # 进入安装目录
    $ cd /root/nginx-1.24.0# 预编译配置
    $ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module# 增加TCP\UDP负载均衡模块
    $ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-stream# 编译并安装
    $ make && make install
    

    安装说明: 考虑后续可能会添加 ssl 证书和 stream 负载均衡,故在初始化时安装 3 个模块

    1. with-http_ssl_module:启用 nginx 的 https 功能,即支持 SSL/TLS 加密协议,使 Nginx 能够处理加密连接,实现安全的加密通信。
    2. with-http_stub_status_module:启用 nginx 的 Stub Status 模块,提供了一个简单的状态页面,可以监控 nginx 的运行状态,如连接数、请求处理情况等。
    3. with-stream:用于实现 TCP 和 UDP 的负载均衡、代理等功能。Stream 模块使 Nginx 能够处理更多类型的流量,而不仅仅是 HTTP 流量。
  • 查看 nginx 是否安装成功

    # 查看 nginx 版本
    $ /usr/local/nginx/sbin/nginx -V
    nginx version: nginx/1.24.0
    built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
    configure arguments: --prefix=/usr/local/nginx
    
  • 管理 nginx 服务,启动、停止、加载

    # 启动nginx
    $ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf# 停止nginx
    $ /usr/local/nginx/sbin/nginx -s stop# 重载nginx
    $ /usr/local/nginx/sbin/nginx -s reload# 杀掉nginx
    $ /usr/local/nginx/sbin/nginx -s quit# 检查nginx配置
    $ /usr/local/nginx/sbin/nginx -t
    

2.3 开机自启 nginx

  • 创建 service 文件,设置 nginx 开机自启服务

    $ vim /lib/systemd/system/nginx.service
    [Unit]
    Description=nginx service
    After=network.target [Service] 
    Type=forking 
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s quit
    PrivateTmp=true [Install] 
    WantedBy=multi-user.target$ systemctl daemon-reload
    $ systemctl start nginx.service          启动nginx服务
    $ systemctl stop nginx.service           停止服务
    $ systemctl restart nginx.service        重新启动服务$ systemctl list-units --type=service     查看所有已启动的服务
    $ systemctl status nginx.service          查看服务当前状态
    $ systemctl enable nginx.service          设置开机自启动
    $ systemctl disable nginx.service         停止开机自启动
    

三、安装 openresty 服务

3.1 安装前环境准备

  • 安装 openresty 编译所需相关依赖

    # 安装相关依赖
    $ yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel# 查询版本信息
    $ gcc --version# 查询是否安装成功
    $ rpm -qa pcre pcre-devel zlib zlib-devel openssl openssl-devel
    

3.2 源码安装 openresty

  • 上传 nginx 压缩包,并解压

    # 在线下载
    $ wget https://openresty.org/download/openresty-1.25.3.2.tar.gz# 解压安装包
    $ tar -zxvf openresty-1.25.3.2.tar.gz
    $ ll -h
    drwxrwxr-x 5 root root  130 Jul 17 04:52 openresty-1.25.3.2
    -rw------- 1 root root 5.6M Sep 19 09:39 openresty-1.25.3.2.tar.gz
    
  • 进入安装目录,编译安装 openresty

    # 进入安装目录
    $ cd openresty-1.25.3.2# 预编译配置
    $ ./configure --prefix=/usr/local/openresty # 可自定义添加模块
    $ ./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module
    # --prefix 指定安装目录,--with-cc-opt 和 --with-ld-opt 指定自定义的库路径# 编译安装 (二选一)
    $ make && make install
    $ gmake && gmake install
    
  • 添加 nginx 代理缓存清理工具 (可选)

    # 添加nginx缓存插件
    $ wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz$ tar -zxvf ngx_cache_purge-2.3.tar.gz $ mkdir /usr/local/openresty/modules$ mv ngx_cache_purge-2.3 /usr/local/openresty/modules/$ ./configure --prefix=/usr/local/openresty \
    --with-luajit \
    --without-http_redis2_module \
    --with-http_stub_status_module \ 
    --with-http_v2_module \
    --with-http_gzip_static_module \
    --with-http_sub_module \
    --add-module=/usr/local/openresty/modules/ngx_cache_purge-2.3
    

    安装说明:

    1. with-luajit:安装luajit相关库,luajit是lua的一个高效版
    2. without-http_redis2_module:现在使用的Redis都是3.x以上版本,不推荐使用Redis2,表示不安装redis2支持的lua库
    3. with-http_stub_status_module:http对应状态的库
    4. with-http_v2_module:对Http2的支持
    5. with-http_gzip_static_module:gzip服务端压缩支持
    6. with-http_sub_module:过滤器,可以将指定的字符串替换为另一个字符串来修改响应
    7. add-module=../modules/ngx_cache_purge-2.3:nginx代理缓存清理工具
  • 查看 openresty 和 nginx 是否安装成功

    $ /usr/local/openresty/nginx/sbin/nginx -v
    nginx version: openresty/1.21.4.4
    
  • 配置 openresty 环境变量

    $ vim /etc/profile 
    $ export PATH=/usr/local/openresty/nginx/sbin:$PATH
    $source /etc/profile
    

3.4 离线安装 openresty

  • 源码方式: 下载 openssl、zlib、pcre 的源码包

    # openssl主要用于ssl模块加密,支持https
    wget https://openssl-library.org/source/old/1.1.1/openssl-1.1.1k.tar.gz# pcre用于实现对地址重定向, 地址重写和localtion指令和正则表达式
    wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.45.tar.gz# zlib用于对http数据进行压缩, gzip压缩模块
    wget https://zlib.net/zlib-1.3.1.tar.gz
    
  • 解压并编译安装 openssl、zlib、pcre

    # 解压、编译、安装openssl
    $ tar -zxvf openssl-1.1.1k.tar.gz
    $ cd openssl-1.1.1k
    $ mkdir -p /usr/local/src/openssl
    $ ./config --prefix=/usr/local/src/openssl --openssldir=/usr/local/src/openssl/ssl --shared
    $ make && make install # 解压、编译、安装pcre
    $ tar -zxvf pcre-8.45.tar.gz
    $ cd pcre-8.45
    $ mkdir -p /usr/local/src/pcre
    $ ./configure --prefix=/usr/local/src/pcre --enable-utf8
    $ make && make install # 解压、编译、安装zlib
    $ tar -zxvf zlib-1.3.1.tar.gz
    $ cd zlib-1.3.1
    $ mkdir -p /usr/local/src/zlib
    $ ./configure --prefix=/usr/local/src/zlib
    $ make && make install
    
  • 系统配置 openssl、pcre、zlib 服务

    # 1. 系统配置
    $ cd /etc/ld.so.conf.d		
    $ echo "/usr/local/src/zlib" > zlib.conf
    $ echo "/usr/local/openssl/lib" >> /etc/ld.so.conf# 2. 加载配置
    $ ldconfig# 3. 查看版本
    $ cat /usr/local/src/zlib/lib/pkgconfig/zlib.pc
    $ openssl version -a
    $ pcre-config --version
    
  • 进入安装目录,编译安装 openresty

    $ ./configure --prefix=/usr/local/openresty \
    --with-openssl=/usr/local/src/openssl \
    --with-pcre=/usr/local/src/pcre \
    --with-zlib=/usr/local/src/zlib \
    --with-luajit \
    --without-http_redis2_module \
    --with-http_iconv_module \
    --with-http_stub_status_module \
    --with-stream \
    --with-http_ssl_module$ make && make install
    
  • RPM 方式: 下载 openssl、zlib、pcre 的 rpm 包

    # 在可以连接外网的服务器上执行下载rpm
    $ yum -y install --downloadonly --downloaddir=/opt gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel# downloadonly:只下载软件包而不安装
    # downloaddir:指定下载的软件包存储目录
    tar -zcvf src.tar.gz /opt/*.rpm
    
  • 安装 openssl、zlib、pcre 包

    # 上传至内网服务器后解压安装依赖
    $ cd /opt
    $ tar -zxvf src.tar.gz
    $ yum -y localinstall *.rpm# 解压安装包
    $ tar -zxvf openresty-1.25.3.2.tar.gz
    $ ll -h
    drwxrwxr-x 5 root root  130 Jul 17 04:52 openresty-1.25.3.2
    -rw------- 1 root root 5.6M Sep 19 09:39 openresty-1.25.3.2.tar.gz# 进入安装目录
    $ cd openresty-1.25.3.2# 预编译配置
    $ ./configure --prefix=/usr/local/openresty --with-luajit --without-http_redis2_module --with-http_iconv_module --with-stream --with-http_ssl_module# 编译安装
    $ gmake && gmake install
    

3.5 开机自启 openresty

  • 创建 service 文件,设置 nginx 开机自启服务

    $ vim /usr/lib/systemd/system/openresty.service
    [Unit]
    Description=openresty web service
    Documentation=https://openresty.org/cn/
    After=network.target[Service]
    Type=forking
    PIDFile=/usr/local/openresty/nginx/logs/nginx.pid
    ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf
    ExecStart=/usr/local/openresty/nginx/sbin/nginx
    ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
    PrivateTmp=true[Install]
    WantedBy=default.target$ systemctl daemon-reload
    $ systemctl start openresty		# 启动服务	
    $ systemctl stop openresty		# 停止服务
    $ systemctl restart openresty	# 重启服务
    $ systemctl reload openresty	# 重新加载配置文件
    $ systemctl status openresty	# 查看nginx状态
    $ systemctl enable openresty	# 开机启动# 查看nginx是否启动
    $ ps -ef | grep openresty
    

四、nginx 常用配置

4.1 nginx 基础配置

  • nginx 代理的简单配置

    server {listen       80;server_name  localhost;location / {root   /usr/local/web/dist;index  index.html index.htm;# 如果配置的是history路由需开启此项# try_files $uri $uri/ /index.html;}# 静态资源目录,在对应目录先建好文件夹location /admintest {alias   /usr/local/web/admin/dist;index  index.html index.htm;}#代理node服务location /api {proxy_pass http://127.0.0.1:3002;}
    }
    
  • 配置说明:root 指令 和 alias 指令

    1. root指令:用于指定服务器文件的根目录,nginx会将location后面的uri与root指令指定的路径拼接起来,作为最终的文件路径。
    2. alias指令:用于指定服务器文件的别名,nginx会将location后面的uri替换为alias指令指定的路径,作为最终的文件路径。
    # nginx配置示例: 
    location /dist/ {root /opt/nginx/html;index index.html index.htm;try_files $uri $uri/ /index.html;
    }# root是拼接: 当请求/dist/static/css/style.css时, nginx会将其映射到/opt/nginx/html/dist/static/css/style.css文件上
    # alias是替换: 当请求/dist/static/css/style.css时, nginx会将其映射到/opt/nginx/html/static/css/style.css文件上
    

4.2 nginx 重定向

  • nginx 重定向配置

    # if按照格式书写,括号前后的空格必须带着
    server {listen 80;# listen 443;server_name www.xxx.com xxx.com;if ($host != 'www.xxx.com') {rewrite ^/(.*)$ http://www.xxx.com/$1 permanent;}
    }server {server_name  xxx.com;rewrite ^(.*)$ http://www.xxx.com$1 permanent;
    }# (提前配置好SSL证书)
    server {listen 80;server_name www.xxx.com xxx.com;rewrite ^(.*)$ https://$host$1;
    }
    

4.3 nginx 跨域访问

  • nginx 跨域访问配置

    location / {  # 允许跨域的请求,可以自定义变量$http_origin,*表示所有  add_header 'Access-Control-Allow-Origin' *;  # 允许携带cookie请求  add_header 'Access-Control-Allow-Credentials' 'true';  # 允许跨域请求的方法:GET,POST,OPTIONS,PUT  add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT';  # 允许请求时携带的头部信息,*表示所有  add_header 'Access-Control-Allow-Headers' *;  # 允许发送按段获取资源的请求  add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';  # 一定要有!!!否则Post请求无法进行跨域!  # 在发送Post跨域请求前,会以Options方式发送预检请求,服务器接受时才会正式请求  if ($request_method = 'OPTIONS') {  add_header 'Access-Control-Max-Age' 1728000;  add_header 'Content-Type' 'text/plain; charset=utf-8';  add_header 'Content-Length' 0;  # 对于Options方式的请求返回204,表示接受跨域请求  return 204;  }  
    } 
    

4.4 nginx 防盗链

  • nginx 静态资源防盗链配置

    # 配置基础语法 
    valid_referers none | blocked | server_names | string ...;
    1. none:表示接受没有Referer字段的HTTP请求访问
    2. blocked:表示允许http://或https//以外的请求访问
    3. server_names:资源的白名单,这里可以指定允许访问的域名
    4. string:可自定义字符串,支配通配符、正则表达式写法# 在动静分离的location中开启防盗链机制  
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css){  # 最后面的值在上线前可配置为允许的域名地址  valid_referers blocked 192.168.12.129;  if ($invalid_referer) {  # 可以配置成返回一张禁止盗取的图片  # rewrite   ^/ http://xx.xx.com/NO.jpg;  # 也可直接返回403  return   403;  }  root   /soft/nginx/static_resources;  expires 7d;  
    }  
    

    说明: nginx 也有防盗链的相关模块 ngx_http_accesskey_module

4.5 nginx 证书配置

  • nginx 配置 ssl 证书 及相关 https 的配置

    # ----------HTTPS配置-----------  
    server {  # 监听HTTPS默认的443端口  listen 443;  # 配置自己项目的域名  server_name www.xxx.com;  # 打开SSL加密传输  ssl on;  # 输入域名后,首页文件所在的目录  root html;  # 配置首页的文件名  index index.html index.htm index.jsp index.ftl;  # 配置自己下载的数字证书  ssl_certificate  certificate/xxx.pem;  # 配置自己下载的服务器私钥  ssl_certificate_key certificate/xxx.key;  ssl_session_cache    shared:SSL:1m;# 停止通信时,加密会话的有效期,在该时间段内不需要重新交换密钥  ssl_session_timeout 5m;  # TLS握手时,服务器采用的密码套件  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;  # 服务器支持的TLS版本  ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;  # 开启由服务器决定采用的密码套件  ssl_prefer_server_ciphers on;  location / {  ....  }  
    }  # ---------HTTP请求转HTTPS-------------  
    server {  # 监听HTTP默认的80端口  listen 80;  # 如果80端口出现访问该域名的请求  server_name www.xxx.com;  # 将请求改写为HTTPS(这里写你配置了HTTPS的域名)  rewrite ^(.*)$ https://www.xxx.com;  
    }  
    

4.6 nginx 动静分离

  • nginx 配置动静分离

    # 在项目上线部署时,这些静态资源会img|html|js|css...一起打成包
    # 那这对于后端服务器的压力是尤为巨大的# 1. 先在部署Nginx的机器,Nginx目录下创建一个目录static_resources
    mkdir static_resources  # 2. 将项目中所有的静态资源全部拷贝到该目录下,而后将项目中的静态资源移除重新打包# 3. 修改一下nginx.conf的配置,增加一条location匹配规则
    location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css){  root   /soft/nginx/static_resources;  expires 7d;  
    }  # 也可以将静态资源上传到文件服务器中,然后location中配置upstream指向
    

4.7 nginx 资源压缩

  • nginx 资源压缩配置

    # 在Nginx也提供了三个支持资源压缩的模块
    ngx_http_gzip_module
    ngx_http_gzip_static_module
    ngx_http_gunzip_module# 相关配置如下
    http{# 开启压缩机制gzip on;# 指定会被压缩的文件类型(也可自己配置其他类型)gzip_types text/plain application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;# 设置压缩级别,越高资源消耗越大,但压缩效果越好gzip_comp_level 5;# 在头部中添加Vary: Accept-Encoding(建议开启)gzip_vary on;# 处理压缩请求的缓冲区数量和大小gzip_buffers 16 8k;# 对于不支持压缩功能的客户端请求不开启压缩机制gzip_disable "MSIE [1-6]\."; # 低版本的IE浏览器不支持压缩# 设置压缩响应所支持的HTTP最低版本gzip_http_version 1.1;# 设置触发压缩的最小阈值gzip_min_length 2k;# 关闭对后端服务器的响应结果进行压缩gzip_proxied off;
    }
    

4.8 nginx 负载均衡

  • nginx 反向代理-负载均衡

    upstream myserver{  # 30s内检查心跳发送两次包,未回复就代表该机器宕机,请求分发权重比为1:2  server xxx:8080 weight=100 max_fails=2 fail_timeout=30s;   server xxx:8090 weight=200 max_fails=2 fail_timeout=30s;  # 这里的IP请配置成你WEB服务所在的机器IP  
    }  server {  location / {  root   html;index  index.html index.htm index.ftl;  proxy_set_header Host $host;  proxy_set_header X-Real-IP $remote_addr;  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 请求交给名为nginx_boot的upstream上  proxy_pass http://myserver;  }  
    }  
    

4.9 nginx 代理访问

  • 场景说明: 在某些政务单位或机构,内部使用的是局域网即所谓的内网环境,无法直接访问外网即互联网环境,访问外网必须通过指定的出口服务器,即可以访问外网的服务器,才能正常访问互联网。现需要在内网环境访问互联网的域名地址,如访问www.baidu.com,则需要通过代理 IP 和端口才能去访问外网的域名。

  • 配置说明: 内网通过指定服务器的172.18.36.20:80访问外网域名api.server.com

    1. 正向代理: 正向代理代表客户端向服务器发送请求, 隐藏客户端身份
    2. 反向代理: 反向代理代表服务器向客户端发送响应, 隐藏服务器身份
    # 根据使用场景, 我们的PC电脑接入的是政务机构的局域网(内网)环境, 要想访问外网域名api.server.com, 可以在内网环境的电脑主机通过浏览器访问172.18.36.20这台服务器的80端口从而访问到域名网址, 这种方式向互联网隐藏了客户端的身份信息, 属于正向代理,说明:此处的api.server.com只是个虚构的域名。# NGINX配置
    server {listen       80;server_name  172.18.36.20;# 访问http://172.18.36.20:80/代理访问https:/api.server.com:443location / {proxy_send_timeout 600;proxy_read_timeout 600;proxy_connect_timeout 600;proxy_redirect off;proxy_set_header Host api.server.com;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass https://api.server.com:443/;add_header Access-Control-Allow-Origin *;}# 其他示例:访问http://172.18.36.20:80/map代理访问https://api.hlw.com:443/maplocation /map {proxy_send_timeout 600;proxy_read_timeout 600;proxy_connect_timeout 600;proxy_redirect off;proxy_set_header Host api.hlw.com;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass https://api.hlw.com:443/map;add_header Access-Control-Allow-Origin *;}
    }
    

5.0 nginx 配置简介

  • nginx 基础配置及说明

    # user  nobody;#运行nginx的默认账号
    # nginx进程数,建议设置为等于CPU总核心数。
    worker_processes  1;# 事件区块开始
    events {# 单个进程最大链接数(最大连接数=连接数*进程数)# 根据硬件调整,与前面工作进程配合起来用,尽量大,但别把CPU跑到100%就行,每个进程允许的最多连接数,理论上为每台nginx服务器的最大连接数worker_connections  1024;
    }# 设定http服务器,利用它的反向代理功能提供负载均衡支持
    http {# include:导入外部文件mime.types,将所有types提取为文件,然后导入到nginx配置文件中。include       mime.types;# 默认文件类型default_type  application/octet-stream;# 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设置为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载,注意:如果图片显示不正常把这个改成off# sendfile指令指定,nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设为on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptimesendfile        on;# 长连接超时事件,单位是秒keepalive_timeout  65;# 第一个server区块开始,表示一个独立的虚拟主机站点server {# 提供服务的端口,默认80listen       80;# 提供服务的域名主机名server_name  localhost;# 对 “/” 启动反向代理,第一个location区块开始location / {root   html;    # 服务默认启动目录,可以改成指定的目录位置index  index.html index.htm; # 默认的首页文件,多个用空格分开}# 错误页面路由# 出现对应的http状态码是,使用50x.html回应客户error_page   500 502 503 504  /50x.html; # location区块开始,访问50x.htmllocation = /50x.html {# 指定对应的站点目录为htmlroot   html; }}
    }
    
  • nginx 其他配置及说明

    $ remote_addr 与 $ http_x_forwarded_for 用以记录客户端的ip地址
    $ remote_user:用来记录客户端用户名称
    $ time_local: 用来记录访问时间与时区
    $ request: 用来记录请求的url与http协议
    $ status: 用来记录请求状态;成功是200
    $ body_bytes_sent:记录发送给客户端文件主体内容大小
    $ http_referer:用来记录从那个页面链接访问过来的
    $ http_user_agent:记录客户端浏览器的相关信息
    

五、安装配置问题

5.1 问题 1:编译报错

  • 问题说明 : make 编译时,出现报错configure: error: Invalid C++ compiler or C++ compiler flags 信息

  • 解决办法: 这是因为系统缺失 gcc-c++ 库导致的,安装 gcc-c++依赖,重新编译安装 nginx 即可

    $ yum install gcc-c++
    $ make clean
    $ ./configure --prefix=/usr/local/openresty \
    --with-openssl=/usr/local/src/openssl \
    --with-pcre=/usr/local/src/pcre \
    --with-zlib=/usr/local/src/zlib \
    --with-luajit \
    --without-http_redis2_module \
    --with-http_iconv_module \
    --with-http_stub_status_module \
    --with-stream \
    --with-http_ssl_module$ make && make install
    

5.2 问题 2:配置报错

  • 问题说明 : 如若启动 nginx 时,出现关于 ssl 的报错unknown directive "ssl"

  • 解决办法: 导致出现这种问题的原因有如下两种:

    • 方法 1: 查看 server 证书配置 ssl on 改为 listen 443 ssl
    • 方法 2: 查看预编译是否添加了 with-http_ssl_module 模块
    $ /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/nginx.conf:xx# 解决办法1
    # 查看原本配置
    $ vim /usr/local/nginx/conf/nginx.conf
    ...
    server {  listen 443;  server_name www.xxx.com;   ssl on; 		# 打开SSL加密传输 ssl_certificate  certificate/xxx.pem;  ssl_certificate_key certificate/xxx.key;  ...
    }  
    ...# 修改NGINX配置
    server {  listen 443 ssl;server_name www.xxx.com;  ssl_certificate  certificate/xxx.pem;  ssl_certificate_key certificate/xxx.key;  ...
    }  # 解决办法2 
    # 1. 增加--with-http_ssl_module模块
    $ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-stream# 2. 编译安装, make install会覆盖原本安装的内容
    $ make# 3. 替换nginx程序
    $ cp -rpf /root/nginx-1.24.0/objs/nginx /usr/local/nginx/sbin/nginx
    # -r:递归复制整个目录树 -f:强制复制不给予提示 -p:保留源文件或目录的属性# 4. 验证nginx版本
    $ /usr/local/nginx/sbin/nginx -V
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/57010.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

执行vue create XXX报错The operation was rejected by your operating system

创建项目: vue create my-project 报错: npm ERR! code EPERM npm ERR! syscall open npm ERR! path D:\Program Files\nodejs\node_cache\_cacache\tmp\5d2a6f8e npm ERR! errno -4048 npm ERR! Error: EPERM: operation not permitted, open D:\Pro…

Svan.的创作纪念日

机缘 大家好,今天是我成为CSDN创作者的1024天,作为一名算是资深社区的博主,我有太多的话想和大家说,但是近几天真的很忙,所以我只用最简洁的话来说一下我一路走来的创作经验和感受!! 首先说一…

如何配置 Jenkins 主从架构以及结合 Gerrit 和镜像操作

Jenkins Master-Slave 配置 Gerrit 集成 Jenkins Gerrit 镜像操作 一、Jenkins Master-Slave 配置 主 Jenkins(Master)配置: 安装 Jenkins 并启动服务。访问 Jenkins Web UI,完成初始设置。 下载并安装“SSH Slaves”插件&#x…

界面组件DevExpress WPF v24.1亮点 - 支持全新的字体图标图像

DevExpress WPF拥有120个控件和库,将帮助您交付满足甚至超出企业需求的高性能业务应用程序。通过DevExpress WPF能创建有着强大互动功能的XAML基础应用程序,这些应用程序专注于当代客户的需求和构建未来新一代支持触摸的解决方案。 DevExpress WPF控件日…

移情别恋c++ ദ്ദി˶ー̀֊ー́ ) ——14.哈希(2)(模拟实现)

1.概念介绍 1.1开散列 开散列(Open Hashing),也叫链地址法,是一种解决哈希冲突的方法。每个哈希表槽位保存一个链表,所有散列到同一位置的元素都存储在该链表中。当插入元素发生冲突时,将新元素添加到相应…

Ansible概述

目录 一、ansible简介 二、absible的特点 三、ansible的工作原理以及流程 四、ansible环境安装部署 五、ansible命令行模块 六、inventory 主机清单 一、ansible简介 Ansible是一个基于Python开发的配置管理和应用部署工具,现在也在自动化管理领域大放异彩。…

HCIP-HarmonyOS Application Developer 习题(十二)

(多选)1、声明式开发范式的转场动画包含以下哪几种类型? A、页面间转场 B、应用间转场 C、共享元素转场 D、组件内转场 答案:ACD 分析: (多选)2、公共事件服务为应用程序提供哪些能力。 A、取消发布公共…

基于STM32设计的养殖场环境监测系统(华为云IOT)

文章目录 一、前言1.1 项目介绍【1】项目开发背景【2】设计实现的功能【3】项目硬件模块组成【4】需求总结 1.2 设计思路1.3 系统功能总结1.4 开发工具的选择【1】设备端开发【2】上位机开发 二、部署华为云物联网平台2.1 物联网平台介绍2.2 开通物联网服务2.3 创建产品&#x…

uniapp下载在线文档并打开

const showUserAgremention () > { uni.showLoading({ title: "正在打开...", }) uni.downloadFile({ url: "https://door.melifego.net/userAgreement.docx&quo…

PMP--必刷题–解题–121-130

文章目录 14.敏捷--产品待办事项列表121、 [单选] 项目经理使用混合型方法来遵守监管要求。规划和收尾阶段将使用预测型方法,而执行阶段将使用迭代方法。在第二次冲刺评审期间,项目发起人要求对一些产品待办事项列表的优先级进行变更。作为服务型&#x…

开源项目 - 轻量级人体姿态 人体关键点检测 机器视觉 深度学习

开源项目 - 轻量级人体姿态 人体关键点检测 机器视觉 深度学习 项目地址:https://gitcode.net/EricLee/light_pose 1、数据集来源:coco2017 数据集 * coco 数据集官方网站:https://cocodataset.org/#home * [数据集下载地址(百度网盘 Pa…

centos8配置java环境变量jdk8u422-b05

1. 下载 JDK 8u422-b05 首先,确保已经下载了 JDK 8u422-b05 的二进制文件。如果还没有下载,你可以去 Oracle 官方网站或者其他可信的源下载 JDK 8u422。 2. 安装 JDK 将下载的 JDK 文件解压到 /usr/local/java 目录下: sudo mkdir /usr/l…

Golang日志库logrus的介绍与使用

logrus概述 简介 Logrus 是一个流行的 Go 语言日志库,它提供了结构化日志和多种日志级别的功能。Logrus 非常灵活,支持自定义日志格式和输出,被许多 Go 语言项目广泛使用 特点 完全兼容log标准库:Logrus 可以很容易地替换掉log…

力扣2653.滑动窗口的美丽值

给你一个长度为 n 的整数数组 nums ,请你求出每个长度为 k 的子数组的 美丽值 。 一个子数组的 美丽值 定义为:如果子数组中第 x 小整数 是 负数 ,那么美丽值为第 x 小的数,否则美丽值为 0 。 请你返回一个包含 n - k 1 个整数…

挖掘数据金矿:Python中缺失值处理的艺术与技巧

引言 在实际的数据集处理过程中,我们经常会遇到各种各样的缺失值问题。这些问题可能是由于数据收集过程中的疏忽,或是数据传输中的丢失造成的。无论是哪种情况,缺失值都会直接影响模型的训练效果和预测准确性。因此,掌握有效的缺…

Java项目-基于springcloud框架的分布式架构网上商城系统项目实战(附源码+文档)

作者:计算机学长阿伟 开发技术:SpringBoot、SSM、Vue、MySQL、ElementUI等,“文末源码”。 开发运行环境 开发语言:Java数据库:MySQL技术:SpringBoot、Vue、Mybaits Plus、ELementUI工具:IDEA/…

​​【项目建设PPT模板】中台建设,中台设计,数字中台整体建设方案(PPT)

工业互联网数字中台解决方案旨在为企业提供全面、高效的数据驱动能力。该方案主要包括以下几个核心部分: 数据中台:作为核心,数据中台负责汇聚、整合、提纯和加工各类工业数据,实现数据资产的标准化、模型化和模块化。通过提供API…

对Kotlin在Android开发中的应用看法

Kotlin在Android开发中的应用:深度解析与前景展望 自Google于2017年宣布Kotlin成为Android开发的官方语言以来,Kotlin在Android开发者社区中迅速崛起,成为与Java并肩而行的主流编程语言。这一变化不仅反映了技术发展的趋势,也体现…

【Pycharm】显示内存不足the IDE is running low on memory解决方法

Pycharm提示显示内存不足the IDE is running low on memory解决方法 在右上角找到Help,点击,找到change memory settings 修改数值如1024,2048 等,增大容量即可。最后点击save and Restart

C++学习,标准库 <regex>

C11 引入了 <regex> 标准库&#xff0c;用于处理正则表达式。这个库提供了一系列类和函数&#xff0c;使得在 C 中进行复杂的字符串匹配、搜索和替换操作变得更加方便和高效。正则表达式是一种强大的文本处理工具&#xff0c;广泛应用于数据验证、文本分析和模式匹配等领…