欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 社会 > nginx

nginx

2025/10/19 9:26:32 来源:https://blog.csdn.net/2402_84844434/article/details/140128520  浏览:    关键词:nginx

nginx.conf/配置文件的作用

1、全局模块

worker_processes 1;

工作进程数,设置成服务器内核数的2倍(一般不超过8个,超过8个会降低性能)

#user nobody;

默认的程序用户就是nginx,这个无需注释无需修改

pid /usr/local/nginx/run/nginx.pid;

pid文件的位置

处理进程的过程必然涉及配置文件和展示页面,也就是涉及打开文件的数量。

*soft nproc 65535

能打开的最大进程的软限制是65535,65535是最大数

*hard nproc 65535

*soft nofile 65535

进程打开文件数的最大值65535

*hard nofile 65535

配置要生效只能重启,这是系统初始化的一个环节。

#网页匹配的工作目录的地址和支持打开页面的文件类型。location / {root   html;
#家目录。nginx工作目录的家目录 /usr/local/nginx/html
#alias也是指匹配nginx的目录  alias要绝对路径index  index.html index.htm;}

面试题

root和alias之间匹配工作目录的区别?

root的匹配模式是拼接

root的工作目录访问的uir /xy102

location /xy102

/opt/test1/

/opt/test1/xy102/

alias匹配nginx的工作目录,路径是绝对路径

location/xy102

alias/opt/test1/xy102/;

alias只能写在http模块当中server模块的location模块里面

root可以写在server模块,也可以在http,也可以location中

alias匹配工作目录,不能使用重定向功能

全局模块

work_proccesses 1;指定进程数

events模块决定了能够处理的连接数

stream 四层代理模块

http模块转发和处理http请求,静态页面,设置代理(正向代理,反向代理)缓存,定义日志格式,重定向配置

在http模块http里面可以有多个server模块

在server模块当中包含:

location模块

在server当中可以有多个location

实验

1、统计访问状态

Active connections:1
当前活动的连接数
server accepts handled requests
已经处理的连接数
36 36 36
三个数字从左往右依次是:已经处理的连接数;成功建立的连接数;已经处理的请求数
Reading:0 Writing:1Waiting:0
服务端正在从客服端读取请求数据
服务端正在把响应数据发送给客服端
有连接处于空闲状态等待新的请求

2、基于密码的授权进行访问控制

[root@test2 conf]# yum -y install httpd-tools
 [root@test1 conf]# vim nginx.conflocation / {root /opt/test1;index index.html;auth_basic "secret";
#开启用户密码验证auth_basic_user_file /usr/local/nginx/passwd/db;
#使用指定的加密文件
}
[root@test1 conf]# 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@test1 conf]# systemctl restart nginx
在网页访问192.168.65.10

3、基于客户端的访问控制根据ip地址进行控制

[root@test1 conf]# vim nginx.conflocation / {root /opt/test1;index  index.html index.htm;deny 192.168.65.20;allow all;}

4、基于域名的nginx主机

[root@test1 conf]# mkdir /var/www/html/wzs
[root@test1 conf]# echo "我真帅!" > /var/www/html/wzs/index.html
server {listen 80;server_name www.wzs.com;charset utf-8;access_log logs/www.wzs.com.access.log;location / {root /var/www/html/wzs;index index.html;
}
error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}
}
映射
[root@test1 conf]# vim /etc/hosts
​
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.65.10 www.wzs.com
[root@test1 conf]# curl www.wzs.com
我真帅!

5、基于ip地址实现多个虚拟主机

[root@test1 conf]# vim nginx.conf
location / {root   html;index  index.html index.htm;deny 192.168.65.20;#allow all;}
[root@test2 conf]# curl 192.168.65.10
<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.22.0</center>
</body>
</html>

6、基于端口实现多个虚拟主机

[root@test2 conf]# vim nginx.conf
#给每个服务设置其对应的端口
server {listen       192.168.11.137:80;server_name  www.xy102.com;charset utf-8;access_log logs/www.xy102.com.access.log;location / {root   html;index  index.html index.htm;}
​server {listen   192.168.11.199:80;server_name www.flq.com;charset utf-8;access_log logs/www.flq.com.access.log;location / {root /var/www/html/flq;index index.html;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
[root@test2 conf]# 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@test2 conf]# systemctl restart nginx   #重启服务
[root@test2 conf]# curl 192.168.11.199:6666
牛啊
[root@test2 conf]# curl 192.168.11.137:8888
nginx默认HTML页面

7、多个主配置文件

​
[root@test1 nginx]# mkdir conf.d
[root@test1 nginx]# ls
client_body_temp  conf.d        html  passwd.db   run   scgi_temp
conf              fastcgi_temp  logs  proxy_temp  sbin  uwsgi_temp
​
[root@test1 opt]# mkdir -p conf/test1
[root@test1 opt]# mkdir -p conf/test2
​
[root@test1 conf]# echo "this is test1" > test1/index.html
[root@test1 conf]# echo "this is test2" > test2/index.html
[root@test1 nginx]# cd conf.d
[root@test1 conf.d]# ls
test1.conf
[root@test1 conf.d]# vim test1.conf 
server {listen 2024;server_name localhost;location /test1 {
​root /opt/conf/;index index.html;}
}
server {listen 2023;server_name localhost;location /test2 {
​root /opt/conf/;index index.html;}
}
​
[root@test1 conf.d]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:2023            0.0.0.0:*               LISTEN      25668/nginx: master 
tcp        0      0 0.0.0.0:2024            0.0.0.0:*               LISTEN      25668/nginx: master 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      25668/nginx: master 

nginx优化与防盗链

隐藏版本号

[root@test1 conf]# vim nginx.conf
​
http {include       mime.types;include   /usr/local/nginx/conf.d/*.conf;
​
default_type  application/octet-stream; server_tokens off;  #或者on关闭
#关闭本版号
[root@test1 conf]# systemctl restart nginx
​
结果显示的版本号,隐藏后不显示
Server:
nginx/1.22.0

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词