nginx实战

nginx实战

源代码编译安装

安装环境

在系统上安装编译环境

使用

[root@localhost ~]# 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 libpcop-devel xz-devel openssl openssl-devel -y

下载nginx源代码

开始编译安装

#获取nginx源代码
[root@localhost nginx]# wget -c https://nginx.org/download/nginx-1.12.0.tar.gz

#解压缩
[root@localhost nginx]# tar -zf nginx-1.12.0.tar.gz

#进⼊源码⽬录
[root@localhost nginx]# cd nginx-1.12.0

#开始编译三部曲
[root@localhost nginx-1.12.0]# ./configure --prefix=/opt/nginx12/ --with-http_ssl_module --with-http_stub_status_module

[root@localhost nginx-1.12.0]# make && make install

[root@localhost nginx-1.12.0]# echo $?
 0
 
[root@localhost nginx-12]# ls
conf  html  logs  sbin

# nginx 的启动命令,放在 sbin 这个目录下
[root@localhost sbin]# ./nginx
[root@localhost sbin]# netstat -tunlp|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14640/nginx: master 

在这里插入图片描述

nginx实战学习

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

静态网站配置

[root@localhost sbin]# cd ..
[root@localhost nginx-12]# ls
client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@localhost nginx-12]# pwd
/opt/nginx-12
[root@localhost nginx-12]# cd conf/

[root@localhost conf]# 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安装目录下加的一个nginx文件夹
 44             root   html;
 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 

43         location / {
               #nginx通过root指令,确定nginx网页 文件放置哪里
               #这个html是nginx安装目录下加的一个nginx文件夹
 44             root   /opt/zhangsong/;
 45             index  index.html index.htm;
 46         }
 
 [root@localhost conf]# pwd
/opt/nginx-12/conf
[root@localhost conf]# /opt/nginx-12/sbin/nginx -t
nginx: the configuration file /opt/nginx-12//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-12//conf/nginx.conf test is successful

#重新启动
[root@localhost conf]# /opt/nginx-12/sbin/nginx -s reload

[root@localhost conf]# mkdir -p /opt/zhangsong
[root@localhost conf]# vim /opt/zhangsong/index.html
<meta charset=utf8>
静态网站的玩法



在这里插入图片描述

基于端口的多虚拟主机

#这个功能是nginx自带的,只需要修改配置文件
#在nginx.conf 中,出现一个server{}区域配置,就是一个网站
 [root@localhost conf]# vim /opt/nginx-12/conf/nginx.conf
 25     server {
 26         listen       80;
 27         server_name  localhost;
 28         
 29         location / {
 30             root  /opt/zhangsong/;
 31             index  index.html index.htm;
 32         }   
 33         
 34         error_page   500 502 503 504  /50x.html;
 35         location = /50x.html {
 36             root   html;
 37         }   


#第二个虚拟主机
server {
       listen  81;
       server_name localhost;

       location / { 
          root  /opt/asong/;
          index  index.html;
}
}

修改网站1内容
[root@localhost ~]# vim /opt/zhangsong/index.html
[root@localhost opt]# cat /opt/zhangsong/index.html 
<meta charset=utf8>

天天快乐


修改网站2内容
[root@localhost ~]# vim /opt/asong/index.html
[root@localhost ~]# cat /opt/asong/index.html 
<meta charset=utf8>

   学习使人进步

  • 修改完配文件,要重启nginx服务
[root@localhost ~]# /opt/nginx-12/sbin/nginx -t
nginx: the configuration file /opt/nginx-12//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-12//conf/nginx.conf test is successful

[root@localhost ~]# /opt/nginx-12/sbin/nginx -s reload

在这里插入图片描述

在这里插入图片描述

访问日志

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

  • 对于该日志的记录、分析,可以清晰的掌握服务器的动态信息
  • 对用户行为进行检测、分析
    • 能够记录出用户访问的时间、次数、频率
#修改nginx的配置
#nginx.conf的层级关系

  
http {
   #日志功能,对下面网站都适用
    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;


     #我是网站1
     server{
     
     }
     #我是网站2
     server{
     
     }
}

[root@localhost ~]# vim /opt/nginx-12/conf/nginx.conf

#具体注释如下
 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21                        '$status $body_bytes_sent "$http_referer" '
 22                        '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     access_log  logs/access.log  main;

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


#持续监测日志内容的变化   tail -f 命令
[root@localhost conf]# pwd
/opt/nginx-12/conf
[root@localhost conf]# tail -f /opt/nginx-12/logs/access.log 

 

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

nginx代理服务

nginx代理服务的配置

  • 当你访问的个人Linux机器,也就是nginx
  • 却可以拿到另外一个网站的数据内容
[root@localhost conf]# pwd
/opt/nginx-12/conf
[root@localhost conf]# vim nginx.conf

#第二个虚拟主机
    server {
       listen  81;
       server_name localhost;

       location / {
        proxy_pass https://www.baidu.com/;
}

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

在这里插入图片描述

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


[外链图片转存中...(img-I2L7FGOV-1670000608644)]

























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值