1.主配置文件与虚拟主机分离
如果虚拟主机很多的话,进行分离看起来会更方便,还可以按功能、业务进行划分,下面以两个虚拟主机为例。
完整的除去空行和注释后的配置文件:
[root@nginx-01 conf]# egrep -v "#|^$" nginx.conf.bak worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
创建/app/nginx/conf目录下虚拟主机配置目录
mkdir extra
利用server模块创建www和bbs两个虚拟站点
[root@nginx-01 conf]# cat -n nginx.conf [root@nginx-01 conf]# sed -n '10,20p' nginx.conf server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
www站点
[root@nginx-01 conf]# cat extra/www.conf server { listen 80; server_name www.yygg.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
bbs站点
[root@nginx-01 conf]# cat extra/bbs.conf server { listen 80; server_name bbs.yygg.com; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html/bbs; } }
主配置文件配置(nginx.conf)
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; }
检查配置
[root@nginx-01 conf]# /app/nginx/sbin/nginx -t nginx: the configuration file /app/nginx-1.18.0//conf/nginx.conf syntax is ok nginx: configuration file /app/nginx-1.18.0//conf/nginx.conf test is successful
创建站点目录
[root@nginx-01 conf]# mkdir /app/nginx/html/{www,bbs} [root@nginx-01 conf]# echo "http://www.yygg.com" >>/app/nginx/html/www/index.html [root@nginx-01 conf]# echo "http://bbs.yygg.com" >>/app/nginx/html/bbs/index.html [root@nginx-01 conf]# echo "192.168.1.5 www.yygg.com bbs.yygg.com" >>/etc/hosts
启动服务并测试
[root@nginx-01 conf]# /app/nginx/sbin/nginx [root@nginx-01 conf]# curl www.yygg.com http://www.yygg.com [root@nginx-01 conf]# curl bbs.yygg.com http://bbs.yygg.com
2.虚拟主机别名设置
以www站点为例,设置别名
所谓别名就是除了主域名外额外设置一个或多个域名
为www.yygg.com
设置别名yygg.com
。
[root@nginx-01 conf]# cat extra/www.conf server { listen 80; server_name www.yygg.com yygg.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html/www; } }
重启nginx测试
[root@nginx-01 conf]# /app/nginx/sbin/nginx -s reload [root@nginx-01 conf]# cat /etc/hosts 192.168.1.5 www.yygg.com bbs.yygg.com yygg.com [root@nginx-01 conf]# curl yygg.com http://www.yygg.com
3.Nginx status状态信息配置
状态信息记录使用的是`ngx_http_stub_status_module`模块实现
输入/app/nginx/sbin/nginx -V
检查编译是否有上述模块:
nginx version: nginx/1.18.0 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled configure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
创建一个status的虚拟主机,方式参考标题1,status.conf
配置文件如下:
server { listen 80; server_name status.yygg.com; location / { stub_status on; access_log off; } }
主配置文件nginx.conf
追加status虚拟主机配置
sed -i '11 i include extra/status.conf;' nginx.conf
检查语法并重启nginx
/app/nginx/sbin/nginx -t /app/nginx/sbin/nginx -s reload
配置hosts解析
192.168.1.5 status.yygg.com
访问status.yygg.com
查看
[root@nginx-01 conf]# curl status.yygg.com Active connections: 1 server accepts handled requests 4 4 4 Reading: 0 Writing: 1 Waiting: 0
显示结果解析:
Active connections: 1 ##正处理的连接数为1
server ##共处理了4次连接
accepts ##共创建了4次握手
handled requests ##共处理了4次请求
Reading ##读取到客户端的Header信息数
Writing ##返回给客户端的Header信息数
Waiting ##NGinx已经处理完正在等候下一次请求的指令的驻留连数
4.增加错误日志
error_log语法:
error_log file level;
关键字不变,file是日志存放位置,level是错误日志级别
通常只用warn|error|crit三个级别
配置错误日志配置,在nging.conf
文件中worker_processes 1;
下添加
error_loglogs/error_log;
没错,就这一行!
以上是Nginx安装后常用功能如何配置的详细内容。更多信息请关注PHP中文网其他相关文章!

NGINX适合处理高并发和静态内容,Apache适用于动态内容和复杂URL重写。1.NGINX采用事件驱动模型,适合高并发。2.Apache使用进程或线程模型,适用于动态内容。3.NGINX配置简单,Apache配置复杂但更灵活。

NGINX和Apache各有优势,选择取决于具体需求。1.NGINX适合高并发,部署简单,配置示例包括虚拟主机和反向代理。2.Apache适用于复杂配置,部署同样简单,配置示例包括虚拟主机和URL重写。

NGINXUnit的目的是简化Web应用程序的部署和管理。其优势包括:1)支持多种编程语言,如Python、PHP、Go、Java和Node.js;2)提供动态配置和自动重载功能;3)通过统一的API管理应用生命周期;4)采用异步I/O模型,支持高并发和负载均衡。

NGINX始于2002年,由IgorSysoev开发,旨在解决C10k问题。1.NGINX是高性能Web服务器,基于事件驱动的异步架构,适用于高并发。2.提供反向代理、负载均衡和缓存等高级功能,提升系统性能和可靠性。3.优化技巧包括调整worker进程数、启用Gzip压缩、使用HTTP/2和安全配置。

NGINX和Apache在架构上的主要区别在于:NGINX采用事件驱动、异步非阻塞模型,而Apache使用进程或线程模型。1)NGINX通过事件循环和I/O多路复用机制高效处理高并发连接,适合静态内容和反向代理。2)Apache采用多进程或多线程模型,稳定性高但资源消耗大,适合需要丰富模块扩展的场景。

NGINX适合处理高并发和静态内容,Apache则适用于复杂配置和动态内容。1.NGINX高效处理并发连接,适合高流量场景,但处理动态内容需额外配置。2.Apache提供丰富模块和灵活配置,适合复杂需求,但高并发性能较差。

NGINX和Apache各有优劣,选择应基于具体需求。1.NGINX适合高并发场景,因其异步非阻塞架构。2.Apache适用于需要复杂配置的低并发场景,因其模块化设计。

NGINXUnit是一个开源应用服务器,支持多种编程语言,提供动态配置、零停机更新和内置负载均衡等功能。1.动态配置:无需重启即可修改配置。2.多语言支持:兼容Python、Go、Java、PHP等。3.零停机更新:支持不中断服务的应用更新。4.内置负载均衡:可将请求分发到多个应用实例。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境