nginx实战

文章目录

nginx实战

  • linux下如何安装一个工具

  • yum工具安装,自动下载nginx,且安装到固定的位置

  • 源代码编译安装,更适合于专业的,企业服务器环境

    • 支持开启nginx的第三方功能,额外的功能
    • 安装路径,配置文件的指定

安装环境

源代码编译安装(该方法适用于其他各种你想编译安装的第三方软件)

  • 下载nginx源代码

  • 在系统上安装好编译环境

  • 开始编译安装

  • 可以使用了

    一. gcc 安装
    安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

    yum install gcc-c++
    二. PCRE pcre-devel 安装
    PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

    yum install -y pcre pcre-devel
    三. zlib 安装
    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

    yum install -y zlib zlib-devel
    四. OpenSSL 安装
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
    nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
    yum install gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel openssl openssl-devel -y

  • 开始下载nginx源码,以及编译安装

    1.下载源码包
    wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

    2.解压缩源码
    tar -zxvf nginx-1.12.0.tar.gz

    3.配置,编译安装 开启nginx状态监测功能
    ./configure --prefix=/opt/nginx1-12/ --with-http_ssl_module --with-http_stub_status_module

    make && make install

    4.启动nginx,进入sbin目录,找到nginx启动命令
    [root@llz ~]#
    [root@llz ~]# cd /opt/nginx1-12/
    [root@llz nginx1-12]#
    [root@llz nginx1-12]#
    [root@llz nginx1-12]#
    [root@llz nginx1-12]# pwd
    /opt/nginx1-12
    [root@llz nginx1-12]#
    [root@llz nginx1-12]# ls
    conf html logs sbin

    nginx的启动命令,就放在sbin这个目录下

    [root@llz sbin]# ./nginx
    [root@llz sbin]#
    [root@llz sbin]#
    [root@llz sbin]# netstat -tunlp |grep nginx
    tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4329/nginx: master

实战主要学习

  • 部署一个静态网站
  • 基于端口的多虚拟主机
  • 访问日志
  • 错误日志
  • 代理服务

静态网站配置

复制代码
# 1,打开nginx配置文件  vim nginx.conf
35     server {
 36         listen       80;
 37         server_name  localhost;
 38
 39         #charset koi8-r;
 40
 41         #access_log  logs/host.access.log  main;
 42         #  这里是nginx的网站配置区域
 43         location / {
 						    # nginx通过root指令,确定nginx的网页文件放在哪里
 						    # 这个html值得是nginx安装目录下加的一个html文件夹
 44             root   /opt/yuchao;
 							  # index参数指的是,首页文件的名字,文件名
 45             index  index.html index.htm;
 46         }
 47
 48         #error_page  404              /404.html;
 49
 50         # redirect server error pages to the static page /50x.html
 51         #
 52         error_page   500 502 503 504  /50x.html;
 53         location = /50x.html {
 54             root   html;
 55         }
 56
  • 修改了配置文件,得重启这个程序,才能更新配置

    先验证配置文件,语法是否正确

    [root@llz conf]# /opt/nginx1-12/sbin/nginx -t
    nginx: the configuration file /opt/nginx1-12//conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx1-12//conf/nginx.conf test is successful

    告诉nginx,你重新读取配置文件

    [root@llz conf]# /opt/nginx1-12/sbin/nginx -s reload

基于端口的多虚拟主机

复制代码
# 这个功能是nginx自己提供的,你只需要修改它的配置文件即可
# 在nginx.conf中,出现一个 server{} 区域配置,就表示一个网站
# 看第一个虚拟主机的配置, server{}
 35     server {
 36         listen       80;
 37         server_name  localhost;
 38
 39         #charset koi8-r;
 40
 41         #access_log  logs/host.access.log  main;
 42
 43         location / {
 44             root   /opt/yuchao/;
 45             index  index.html index.htm;
 46         }
 47
 48         #error_page  404              /404.html;
 49
 50         # redirect server error pages to the static page /50x.html
 51         #
 52         error_page   500 502 503 504  /50x.html;
 53         location = /50x.html {
 54             root   html;
 55         }
 56     }
 
  57 # 第二个虚拟主机,我是韩剧TV的网站
 58 server {
 59         listen 81;
 60         server_name localhost;
 61         location / {
 62         root /opt/chaochao;
 63         index  index.html;
 64  }
 65
 66 }
 67
  • 修改网站1的内容

    [root@llz conf]# cat /opt/yuchao/index.html

    我是视频网站。。

  • 修改网站2的内容

    [root@llz conf]# cat /opt/chaochao/index.html

    我是音乐网站

  • 修改了配置文件,一定要重启nginx服务

    [root@llz conf]# /opt/nginx1-12/sbin/nginx -t
    nginx: the configuration file /opt/nginx1-12//conf/nginx.conf syntax is ok
    nginx: configuration file /opt/nginx1-12//conf/nginx.conf test is successful
    [root@llz conf]#
    [root@llz conf]#
    [root@llz conf]# /opt/nginx1-12/sbin/nginx -s reload

访问日志

nginx能够记录用户的每一次访问请求

  • 对于该日志的记录,分析,可以更清晰的掌握服务器的动态信息,比如安全性
  • 对用户行为进行检测,分析
    • 能够记录出用户访问的时间,次数,频率

      修改nginx的配置如下

      nginx.conf的层级关系

      http{
      # 日志功能写在这里,对下面这些网站都适用

      我是网站1

      复制代码
      server{
      
      }
      # 我是网站2
      server {
      
      }

      }

      具体配置如下

      17 http {
      18 include mime.types;
      19 default_type application/octet-stream;
      20
      21 log_format main 'remote_addr - remote_user [time_local] "request" '
      22 'status body_bytes_sent "http_referer" ' 23 '"http_user_agent" "$http_x_forwarded_for"';
      24
      25 access_log logs/access.log main;
      26

      重启nginx服务

      [root@llz conf]# vim /opt/nginx1-12/conf/nginx.conf
      [root@llz conf]#
      [root@llz conf]# /opt/nginx1-12/sbin/nginx -s reload

      持续的检测,日志内容变化,tail -f命令

      [root@llz conf]# tail -f /opt/nginx1-12/logs/access.log

通过该access.log日志,即可进行更多的日志分析,sed awk,grep这样的命令去实践。

nginx代理服务

nginx代理服务的配置

  • 当你访问你的个人linux机器,也就是nginx

  • 但是却可以拿到另外一个网站的数据内容

    第一个虚拟主机,视频资料网站

    复制代码
      server {
          listen       80;
          server_name  localhost;
    
          #charset koi8-r;
    
          #access_log  logs/host.access.log  main;
    
          location / {
            proxy_pass https://baidu.com;
          }
    
          #error_page  404              /404.html;
    
          # redirect server error pages to the static page /50x.html
          #
          error_page   500 502 503 504  /50x.html;
          location = /50x.html {
              root   html;
          }
      }
相关推荐
ping某17 小时前
为什么 Nginx 明明监听了 80,转发后端时却用了 4xxxx 端口?
后端·nginx
大树883 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠3 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质3 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工3 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智3 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_3 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化
施努卡机器视觉3 天前
SNK施努卡侧滑门锁上滑轮总成自动化装配线,从零件到组件,全流程精密制造方案
运维·自动化·制造
AC赳赳老秦3 天前
用 OpenClaw 搭建服务器故障应急响应系统,自动处理 80% 常见运维故障
android·运维·服务器·python·rxjava·deepseek·openclaw
java_cj3 天前
深入kube-apiserver认证机制:从Bearer Token到mTLS的完整认证链解析
linux·运维·服务器·云原生·容器·kubernetes