nginx基础

Nginx 简介

nginx 是一个高性能的Web和反向代理服务器,它具有非常优越的特性
nginx 能支持高达50000个并发连接数的响应,用于高连接并发情况 使用epol and kqueue作为开发模型
nginx 作为负载均衡服务器:nginx 即可在内部直接支持和PHP程序对外进行服务,也可支持作为HTTP代理服务器对外进行服务
nginx采用C进行编写,不论系统资源开销还是CPU使用效率都比Perlbal要好

Nginx 的优点

高并发连接:官方测试能够支撑5万并发连接,在实际的生产环境中跑到2~3万的并发连接数

内存消耗少:在三万并发连接下,开启的10个nginx进程才消耗150M的内存

配置文件简单、成本低廉、支持Rewrite重写规则(能够根据域名、URL的不同 ,将HTTP请求分到不同的后端服务器群组

Nginx的基本功能

静态资源的Web服务器,能缓存打开的文件描述符
http、smtp、pop3协议的反向代理服务器
缓存加速、负载均衡 支持FastCGI (fpm,LNMP),uWSGI (Python)等 模块化
(非DSO机制),过滤器zip、SSI及图像的大小调整 支持SSL

Nginx 的扩展功能

基于名称和IP的虚拟主机
支持keepalive
支持平滑升级
定制访问日志,支持使用日志缓冲区提高日志存储性能
支持URL重写
支持路径别名
支持基于IP及用户的访问控制
支持速率限制,支持并发数限制

Nginx 的应用类别

使用nginx结合FastCGI运行的PHP、 JSP 、Perl等程序
使用nginx作反代理、负载均衡、过滤规则
使用nginx运行静态HTML网页、图片
nginx与其他新技术的结合应用

nginx的模块分类

nginx的模块从结构上分为核心模块、基础模块和第三方模块
HTTP模块、EVENT模块和MAIL模块等属于核心模块
HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块属于基本模块
HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块属于第三方模块

Nginx的安装

关闭防火墙和selinux

[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0

创建系统用户

[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

安装依赖环境

#需用网络源安装依赖包。
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'   #安装开发包

创建日志存放目录

[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

下载nginx,编译安装

[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz   #解压
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]#  ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc
 -l) && make install

启动nginx

#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/pr
ofile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
[root@localhost ~]# nginx
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port    
LISTEN     0      128          *:111                      *:*
LISTEN     0      128          *:80                       *:*
LISTEN     0      5      192.168.122.1:53                       *:*            
LISTEN     0      128          *:22                       *:*
LISTEN     0      128    127.0.0.1:631                      *:*                
LISTEN     0      100    127.0.0.1:25                       *:*                
LISTEN     0      128    127.0.0.1:6011                     *:*                
LISTEN     0      128         :::111                     :::*
LISTEN     0      128         :::22                      :::*
LISTEN     0      128        ::1:631                     :::*
LISTEN     0      100        ::1:25                      :::*
LISTEN     0      128        ::1:6011                    :::*

访问到nginx 默认页
在这里插入图片描述

访问控制

用于location段
allow :设定允许哪台或哪些主机访问,多个参数间用空格隔开
deny :设定禁止哪台或那些主机访问,多个参数用空格隔开

allow 192.168.1.1/32 172.16.0.0/16;
deny all;       #可写ip或网段

修改配置文件

   location / {
            root   /www;
            index  index.html index.htm index.php;
            deny 192.168.118.100/24;         #拒绝本机
        }

在这里插入图片描述

基于用户认证

auth_basic "欢迎信息"
auth_basic_user_file "/path/to/user_anth_file"
###
user_anth_file内容格式为:
username:password

这里的密码为加密后的密码串,建议用htpasswd来创建此文件

htpasswd -c -m /path/to/.user_auth_file USERNAME

安装httpd-tools.x86_64 0:2.4.6-80.el7.centos.1

[root@nginx ~]# yum -y install httpd-tools
[root@nginx ~]# htpasswd -c -m /usr/local/nginx/dxk dxk    #用户为dxk
New password:
Re-type new password:
Adding password for user dxk
[root@nginx ~]# cat /usr/local/nginx/dxk
dxk:$apr1$GFSNqOA0$DxMTeAfeV.fYX9GPflHKu.     #用户名和加密后的密码

修改nginx的配置文件

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
  location / {
            root   /www;
            index  index.html index.htm index.php;
            #新增这两行
            auth_basic "welcome";       #访问注释信息
            auth_basic_user_file "/usr/local/nginx/dxk";     #登录的目录
        }

重启nginx

[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#语法没有问题
[root@nginx ~]# nginx -s reload

访问nginx,弹出登录页面
在这里插入图片描述
成功登录,可看到nginx默认页
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx基础配置可以在其配置文件nginx.conf中找到。配置文件的路径通常是/nginx/conf/nginx.conf。 在nginx.conf文件中,可以设置许多选项来配置nginx的行为。其中一些基本的配置选项包括: - http块:在配置文件中,http块定义了全局的http配置,包括一些常用的配置项,如mime.types文件的引入、代理配置文件的引入、fastcgi配置文件的引入以及默认的索引文件等。 - server块:在http块中,可以有多个server块,每个server块定义了一个虚拟主机的配置信息。可以在每个server块中指定域名或IP地址,以及监听的端口号等。 - location块:在server块中,可以有多个location块,每个location块定义了一组匹配规则和对应的处理方式。可以通过location块来指定请求的URL匹配规则,并根据规则配置相应的处理方式,如代理请求到其他服务器、处理静态文件等。 此外,nginx还支持在配置文件中引入其他文件,以便更好地组织和管理配置。例如,可以在nginx.conf中使用include指令来引入其他配置文件,如mime.types、proxy.conf和fastcgi.conf。 总结起来,nginx基础配置可以在nginx.conf文件中找到,其中包括http块、server块和location块等配置项。可以使用include指令来引入其他配置文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Nginx的基本配置](https://blog.csdn.net/weixin_46007090/article/details/120105907)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Nginx基础配置](https://blog.csdn.net/weixin_41968982/article/details/123687834)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值