nginx 命令行启动_linux怎么启动nginx服务

后端 (7) 2024-03-22 09:12

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说nginx 命令行启动_linux怎么启动nginx服务,希望能够帮助你!!!。
Nginx中的模块

目录

Nginx目录索引模块

限制访问模块

认证模块

Nginx状态模块

限制连接数模块

限制请求数模块

LNMP架构是什么?

LNMP是一套技术的组合,L=Linux、N=Nginx、M~=MySQL、P~=PHP

1,Nginx目录索引模块(autoindex

配置目录索引模块位置


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第2张

基本配置指令

语法:	autoindex on | off;
默认:	autoindex off;
位置:	http, server,location
启用或禁用目录列表输出。


语法:	autoindex_exact_size on | off;
默认:	autoindex_exact_size on;
位置:	http,server,location
对于 ,指定是否应在目录列表中输出确切的文件大小,或者四舍五入到千字节、兆字节和千兆字节。


语法:	autoindex_format html | xml | json | jsonp;
默认:	autoindex_format html;
位置:	http, server,location
该指令出现在 1.7.9 版中。
设置目录列表的格式。
使用 JSONP 格式时,回调函数的名称由callbackrequest 参数设置。如果参数缺失或值为空,则使用 JSON 格式。
XML 输出可以使用ngx_http_xslt_module模块进行转换 。


语法:	autoindex_localtime on | off;
默认:	autoindex_localtime off;
位置:	http, server,location
对于 HTML格式,指定目录列表中的时间是否应以本地时区或 UTC 输出。

实战

1)查询autoindex模块(如果没查到,看看是不是默认启用)

nginx -V

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第4张
[root@web03 nginx-1.20.1]# https://zhuanlan.zhihu.com/p/configure --help | grep autoindex

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第6张

2)去nginx配置文件里面

[root@web02 ~]# vi /etc/nginx/nginx.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第8张

3)切换到conf.d

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

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第10张

4)添加一个autoindex配置

方法一:

[root@web02 conf.d]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第12张

配置输入以下内容

server {
    server_name game3.test.com;
    listen 80;

    #开启目录索引
     autoindex on;
    #格式化文件大小
    autoindex_exact_size off;
    # 输出的格式
    autoindex_format html;
    # 使用时区
    autoindex_localtime on;

    location / {
      root /usr/share/nginx;
     index index.html;
    }
}

方法二:

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

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第14张

配置输入以下内容

#开启目录索引
autoindex on;
#格式化文件大小
autoindex_exact_size off;
# 输出的格式
autoindex_format html;
# 使用时区
autoindex_localtime on;

上述完毕执行

[root@web02 conf.d]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第16张

5)查询配置是否成功

[root@web02 conf.d]# nginx -t

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第18张

6)重新启动nginx

[root@web02 conf.d]# systemctl restart nginx

7) 编写Windows的host文件

C:\Windows\System32\drivers\etc


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第20张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第22张

8)、测试

输入浏览器

http://game3.test.com

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第24张

2,访问限制模块(allow,deny)

allow : 允许IP访问

deny :禁止IP访问

基本配置指令

语法: allow address | CIDR | unix: | all;
默认:    —
位置:    http, server, location, limit_except
 
语法: deny address | CIDR | unix: | all;
默认:    —
位置:    http, server, location, limit_except

案例1:只允许192.168.15.1来访问。

1、允许192.168.15.1来访问

allow 192.168.15.1;

2、禁止其他所有IP来访问

deny all;

[root@web02 conf.d]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第26张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第28张

案例2:只允许192.168.15.0来访问。

1、允许192.168.15.0来访问

allow 192.168.15.0/24;

2、禁止其他所有IP来访问

deny all;

[root@web02 conf.d]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第26张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第32张

案例3:要求禁止192.168.15.1来访问。

1、禁止192.168.15.1来访问

deny 192.168.15.1;

2、允许其他所有的IP来访问

allow all;

[root@web02 conf.d]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第26张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第36张

测试(因为本机ip192.168.15.1)


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第38张

3.认证模块(auth_basic)

基本配置指令

语法:auth_basicstring|off;
默认:auth_basic off;
位置:http,server,location,limit_except 
使用“HTTP 基本身份验证”协议启用用户名和密码验证。指定的参数用作realm. 参数值可以包含变量(1.3.10、1.2.7)。特殊值off取消了auth_basic从先前配置级别继承的指令的效果。

语法:	auth_basic_user_file file;
默认:	—
位置:	http, server, location,limit_except
指定保存用户名和密码的文件,格式如下:

 评论
名称 1:密码 1
名称2:密码2:评论
名称 3:密码 3
该file名称可以包含变量。

实战

需要先安装htpasswd

yum install httpd-tools -y

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第40张

1)查询auth_basic模块(如果没查到,看看是不是默认启用)

[root@web02 conf.d]# nginx -V

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第42张
[root@web03 nginx-1.20.1]# https://zhuanlan.zhihu.com/p/configure --help | grep auth_basic

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第44张

2)生成密码

[root@web02 conf.d]# htpasswd -c /etc/nginx/auth xuzelin

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第46张

查看面密码

[root@web02 nginx]# cat auth
xuzelin:$apr1$lEp4s..9$KoSaML6KkAXkqlbgUQ3r30

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第48张

3)配置加密

[root@web02 ~]# cd /etc/nginx/conf.d
[root@web02 nginx]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第50张

4)查询配置是否成功

[root@web02 conf.d]# nginx -t

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第18张

5)重新启动nginx

[root@web02 conf.d]# systemctl restart nginx

6)测试


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第54张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第56张

在命令行中的访问方式

curl -H'Host: index.test.com' http://dzg:123456@192.168.15.8

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第58张

4,Nginx状态模块(stub_status)

基本配置指令

语法: stub_status;
默认:    —
位置: server, location
可以从周围位置访问基本状态信息。

实战

1)配置Nginx状态模块

[root@web02 ~]# cd /etc/nginx/conf.d
[root@web02 nginx]# vi autoindex.conf

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第60张

输入以下内容

location /status {
	stub_status;
}

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第62张

2)查询配置是否成功

[root@web02 conf.d]# nginx -t

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第18张

3)重新启动nginx

[root@web02 conf.d]# systemctl restart nginx

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第66张

4)测试


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第68张

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第70张

提供以下状态信息:

Active connections当前活动客户端连接数,包括Waiting连接数。

accepts接受的客户端连接总数。

handled处理的连接总数。通常,accepts除非达到某些资源限制(例如,worker_connections限制),否则该参数值是相同的。

requests客户端请求的总数。

Reading nginx 正在读取请求头的当前连接数。

Writingnginx 将响应写回客户端的当前连接数。

Waiting当前等待请求的空闲客户端连接数。

5,限制连接数模块(ngx_http_limit_conn_module

1、创建一个内存空间存放访问者的IP

2、设置每一个访问者的同时连接次数

基本配置指令

#设置限制的空间
Syntax: limit_conn_zone key zone=name:size;
Default:    —
Context:    http
 
limit_conn_zone     #设置空间的模块
key                 #指定空间存储的内容
zone                #指定空间
=name               #空间名字
:size;              #空间的大小
 
#调用限制的空间
Syntax: limit_conn zone number;
Default:    —
Context:    http, server, location
 
limit_conn          #调用空间的模块
zone                #空间的名字
number;             #指定可以同时连接的次数

实战

1)配置限制连接数模块

[root@web02 conf.d]# vi game2.conf

输入以下内容(编写位置http文件与server文件之间)

http {
    limit_conn_zone $binary_remote_addr zone=addr:10m;

    ...

    server {

        ...

        location /download/ {
            limit_conn addr 1;
        }

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第72张

2)配置各项参数


# 创建一个叫linux的空间,主要用来存放客户端IP,大小给10M
limit_conn_zone $remote_addr zone=linux:10m;

server {
	server_name game2.test.com;
	listen 80;
	location / {
	# 调用linux空间, 限制连接数为1
	limit_conn linux 1;
	limit_req zone=python burst=5;
	root /usr/share/nginx/html5-mario;
	index index.html;
		}
	}

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第74张

3)测试(限制连接数为1)

安装ab命令

yum install httpd-tools -y 

修改housts配置文件

[root@web02 ~]# vi /etc/hosts

输入以下内容

192.168.15.8 game2.test.com

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第76张

去访问game2.test.com

curl game2.test.com

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第78张

开始加压测试

[root@web02 conf.d]# ab -c 200 -n 10000 http://game2.test.com/

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第80张

知识储备

ab : 创建请求的命令,(yum install httpd-tools -y )

-c : 设置并发

-n : 设置请求的总数

6,限制请求数模块(ngx_http_limit_req_module)

1、创建一个内存空间存放访问者的IP

2、设置每一个访问者的同时请求次数

配置基本指令

#设置空间的语法
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default:    —
Context:    http
 
limit_req_zone          #设置空间的模块
key                     #空间存储的内容
zone                    #指定空间
=name                   #空间的名字
:size                   #空间的大小
rate=rate [sync];       #读写速率
 
#调用的语法
Syntax: limit_req zone=name [burst=number] [nodelay | delay=number];
Default:    —
Context:    http, server, location
 
limit_req               #调用控件模块
zone=name               #指定空间=空间的名字
[burst=number]          #允许多请求几次
[nodelay | delay=number]; #延时

实战

1)配置限制请求数模块

[root@web02 conf.d]# vi game2.conf

输入以下内容(编写位置http文件与server文件之间)

# 创建一个叫linux的空间,主要用来存放客户端IP,大小给10M
limit_conn_zone $remote_addr zone=linux:10m;

limit_req_zone $remote_addr  zone=python:10m rate=1r/s;

server {
	server_name game2.test.com;
	listen 80;
	location / {
	# 调用linux空间, 限制连接数为1
	# limit_conn linux 1;
	limit_req zone=python burst=5;
	root /usr/share/nginx/html5-mario;
	index index.html;
		}
	}

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第82张

2)查询配置是否成功

[root@web02 conf.d]# nginx -t

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第18张

3)重新启动nginx

[root@web02 conf.d]# systemctl restart nginx

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第66张

4)开始加压测试

[root@web02 conf.d]# ab -c 200 -n 10000 http://game2.test.com/

nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第88张

5)测试


nginx 命令行启动_linux怎么启动nginx服务_https://bianchenghao6.com/blog_后端_第90张

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。