nginx基础 第一篇

2019-07-25  本文已影响0人  你好_请关照

此篇以Nginx1.16.0为例

一、安装部署nginx三种方法:

1、非官方源安装,直接yum install nginx 即可

2、官方源安装

1)去nginx官网找到官方源方式(nginx.org)
2)在linux主机中/etc/yum.repos.d/目录下创建nginx.repo文件完成如下配置(根据nginx版本不同需要查看官方资料)
[root@web02 ~]# cat /etc/yum.repos.d/nginx.repo 
[nx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@web02 ~]# 
3)使用 yum provides nginx来查看官方支持的nginx版本,然后选择自己想要的版本下载即可
[root@web02 /etc/nginx/conf.d]# yum provides nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
1:nginx-1.8.0-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.8.1-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.10.0-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.10.1-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.10.2-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.10.3-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.12.0-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.12.1-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.12.2-1.el7_4.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.12.2-3.el7.x86_64 : A high performance web server and reverse proxy server
Repo        : epel



1:nginx-1.14.0-1.el7_4.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.14.1-1.el7_4.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.14.2-1.el7_4.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.16.0-1.el7.ngx.x86_64 : High performance web server
Repo        : nx-stable



1:nginx-1.16.0-1.el7.ngx.x86_64 : High performance web server
Repo        : @nx-stable



[root@web02 /etc/nginx/conf.d]# 

yum install nginx-1.16.0-1.el7.ngx.x86_64

3、编译安装

1)在nginx.org 官网下 复制稳定版你想要的稳定版的连接地址 通过wget 下载到本地(wget http://nginx.org/download/nginx-1.16.0.tar.gz
2)解决软件的依赖关系,提前下载以下文件到主机:

pcre-devel --兼容perl语言正则信息--使nginx能够识别后续配置文件中出现的一些正则字符
openssl-devel --解决配置https时出现的问题,没有此包无法部署https
development --必要的开发组包--yum groupinstall development
gcc --C语言编译器--在编译的过程汇总使用
zlib、zilb-devel --解决在Nginx的各种模块中需要使用 gzip 压缩

yum install -y pcre-devel openssl-devel gcc zlib zlib-devel development 

3)解压缩源码包并进行配置

a、解压后进入源码包目录下找到configure的一下可执行文件使用./configure --help 可看到一些模块的解释帮助
--with-xxx 只提供--with 表示默认不安装,需要则添加模块
--without-xxx 只提供--without 的模块表示默认安装
./configure
--prefix=指定程序保存目录
--user=nginx --服务worker进程管理用户
--group=nginx --服务worker进程管理用户组
--with-http_ssl_module -- 实现激活HTTPS访问功能
--with-http_stub_status_module -- 实现激活nginx状态监控功能
--http-log-path --ch成功访问的access.log日志的存放目录
--error-log-path --错误日志的存放目录error.log

b、执行配置
[root@web_test /server/tools/nginx-1.16.0]# ./configure --prefix=/application/nginx-1.16.0 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module

4)、进行编译make,编译完成后不会显示完成状态 可以使用echo $? 查看
编译过程太长,这里不进行显示
[root@web_test /server/tools/nginx-1.16.0]# make
[root@web_test /server/tools/nginx-1.16.0]# echo $?
0
5)进行安装make install
[root@web_test /server/tools/nginx-1.16.0]# make install
[root@web_test /server/tools/nginx-1.16.0]# echo $?
0
6、编译安装时nginx的一些执行方式
[root@web_test /application/nginx-1.16.0/sbin]# ./nginx -h
nginx version: nginx/1.16.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:

  -?,-h         :帮助
  -v            :显示版本并退出
  -V            :显示版本并配置选项,然后退出
  -t            :测试配置和退出
  -T            :测试配置,转储并退出
  -q            :在配置测试期间抑制非错误消息
  -s signal     :向主进程发送信号:停止、退出、重新打开、重新加载
  -p prefix     :设置前缀路径(默认:/application/nginx-1.16.0/)
  -c filename   :设置配置文件(默认:conf/nginx.conf)
  -g directives :从配置文件中设置全局指令

二、nginx中的一些主要的文件

nginx文件说明.png

三、认识配置文件

1、认识一下有关日志切割的文件配置 /etc/logrotate.conf

[root@web_test /etc]# cat /etc/logrotate.conf 
# see "man logrotate" for details
# rotate log files weekly
weekly                  #多久对日志做切割 每日、月、周、对日志进行切割

# keep 4 weeks worth of backlogs
rotate 4                #切割完了之后保留多少个备份。这里默认保存4周的备份  

# create new (empty) log files after rotating old ones
create                  #创建新的日志文件   

# use date as a suffix of the rotated file
dateext                 #以日期作为备份文件的后缀

# uncomment this if you want your log files compressed
#compress               #备份日志文件是否压缩

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d #读取/etc/logrotate.d目录下的一些备份的配置文件。
#以上为全局配置,以下为局部配置(对某个目录下的某个文件进行配置)

# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly                 #每个月进行分割
    create 0664 root utmp   #创建出来的文件的权限、属主、属组分别是什么
    minsize 1M              #备份文件的最小尺寸
    rotate 1                #备份数量
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.
[root@web_test /etc/logrotate.d]# systemctl restart rsyslog.service 
#删除系统日志后,重启rsyslog服务message等系统日志才会重新记录
#nginx的日志也是一样,删除nginx的日志后重启nginx服务日志会自动开始记录

2、nginx配置文件中的一些功能及作用

2.1 nginx主配置文件nginx.conf

[root@web02 /etc/nginx]# cat nginx.conf 
user  nginx;                #指定worker进程用户信息 虚拟用户管理worker;
worker_processes  1;        #指定worker进程数量,增加worker进程数量,增加服务并行处理能力;
#worker进程数量<尽量小于服务器核心数量<最大不能超过服务器核心数量的两倍;

error_log  /var/log/nginx/error.log warn;  #指定错误日志保存的路径 和日志级别; :定义记录错误信息级别:
pid        /var/run/nginx.pid;             #记录nginx运行的pid信息                                     :1. debug  调试级别    产生输出的信息最详细
                                                                                                    :2. info   信息级别    产生输出详细信息
                                                                                                    :3. notice 通知级别    产生输出详细信息
events {              #上面的内容是主区块用于控制整个nginx服务,events事件区块;                       :4. warn   警告级别    产生提示可能有问题信息  (推荐)
    worker_connections  1024;  #一个worker进程可以处理几个连接                                                                      :5. error  错误级别    产生服务错误信息        (推荐)
}                              #总的最大并发连接=worker_connections*worker——processes<系统打开的最大文件数(ulimit -n)                                                                                         :6. crit   严重级别    产生服务错误信息
                                                                                                    :7. alert  极其严重
http {                #HTTP区块,用来指定网站服务进行加载文件信息,访问日志信息,服务优化配置;
    include       /etc/nginx/mime.types;    #include表示加载,这里表示加载媒体资源类型。
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #log_format访问日志,main调用访问日志的格式,容易被access_log调用;
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main; #访问日志的存储路径,及访问日志的格式;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;      #设备tcp通信的方式为长连接;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf; #加载 /etc/nginx/conf.d下所有以.conf结尾的文件;
}
[root@web02 /etc/nginx]# 

2.2 每个网站配置文件,sever模块中的一些内容

    server {
        listen       80;                 # www网站监听端口
        server_name  www.xiaoxi.com;     # 定义你的网站域名信息
        location / {                     #下一篇博客详细介绍location
            root   /html/www;             # 指定站点目录
            index  index.html index.htm;  # 定义首页文件
        }
        error_page   500 502 503 504  /50x.html;   # 错误页面优雅显示配置
        location = /50x.html {                     # 匹配指定uri
            root   /usr/share/nginx/html;          # 设置指点目录
        }
    }

3、企业环境如何使用nginx

3.1 配置多个网站页面环境(比较简单这里不做演示)

第一种方式:通过多个URL来实现 (企业环境常用)
第二种方式:通过端口号实现 (内部平台常用)
第三种方式:通过多个IP地址方式实现 (提升网站安全,指监听内网地址)
注意同一个服务多个网站时

当使用域名+端口来访问网站时,如果能匹配到相应的域名和端口,则正常匹配访问网站;
当使用域名+端口来访问网站时,当域名能够被成功的解析后,但是端口不能匹配响应的域名,然后其它站点的端口能够匹配当前的端口, 这种情况会按照主配置文件加载的站点配置文件的顺序来匹配站点目录;如下

image.png
匹配方式

4、利用web实现文件共享

server {
    listen       80;
    server_name www.xiaoxi.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {                        #location 这里不能有任何首页文件
        root  /etc/nginx/html/;
       # index  index.html index.htm ; #不能有index 类型
        autoindex on ;                 #自动识别index类型
    }

5、开启网站认证功能

第一步:修改配置文件

server {
    listen       80;
    server_name www.xiaoxi.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root  /etc/nginx/html/;
       #index  index.html index.htm ;
        autoindex on ;
        auth_basic           "xiaoxi";          # 在谷歌浏览器上"xiaoxi" 这是个无用的东西,在其他的浏览器上这是个提示的东西
        auth_basic_user_file conf/htpasswd;     #用户验证的密码匹配文件
    }

第二步:生成验证用户和密码(需要安装一个httpd-tools的工具)

[root@web02 /etc/nginx]# htpasswd -bc /etc/nginx/conf/htpasswd xiaoxi xiaoxi123
Adding password for user xiaoxi
[root@web02 /etc/nginx]# cat /etc/nginx/conf/htpasswd 
xiaoxi:$apr1$1uXhlIr3$xTnbUw4cw99cqEyqX5CFK/
[root@web02 /etc/nginx]# htpasswd -b /etc/nginx/conf/htpasswd  xiaoli xiaoli123 #需要追加用户的时候将 c参数去掉即可
Adding password for user xiaoli
[root@web02 /etc/nginx]# cat /etc/nginx/conf/htpasswd 
xiaoxi:$apr1$1uXhlIr3$xTnbUw4cw99cqEyqX5CFK/
xiaoli:$apr1$XQy4rdG4$.a9Hncssy9CVc6DtDecQJ0
[root@web02 /etc/nginx]# 


第三步:访问验证

6、安全策略

对访问用户访问资源进行策略阻止

防止有用户攻击网站/爬网站资源

编写配置文件
   server {
   listen       80;
   server_name www.xiaoxi.com;

   #charset koi8-r;
   #access_log  /var/log/nginx/host.access.log  main;

   location / {
       root  /etc/nginx/html/;
      #index  index.html index.htm ;
       autoindex on ;
       auth_basic           "xiaoxi";          
       auth_basic_user_file conf/htpasswd;     
   }
         location /dev {
             root    /etc/nginx//html/www;
             autoindex on;
             allow 10.0.0.0/24;
             deny 10.0.1.0/24;
         }
         location /sa  {
             root   /etc/nginx//html/www;
             autoindex on;
             deny 10.0.0.0/24;
             allow 10.0.1.0/24;
         }
     }

PS:403状态码出现的原因
01. 对访问用户地址进行阻止
02. 访问的首页文件不存在

上一篇下一篇

猜你喜欢

热点阅读