这篇文章主要介绍了Nginx安装后常用的功能配置,为了在使用中更高效简洁,Nginx安装后通常会进行一些常用的配置,有需要的朋友可以借鉴参考下,希望能够有所帮助
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/HdhCmsTestconf server { listen 80; server_name HdhCmsTestyygg测试数据; 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测试数据; 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/HdhCmsTestconf;include extra/bbs.conf;}
检查配置
[root@nginx-01 conf]# /app/nginx/sbin/nginx -tnginx: the configuration file /app/nginx-1.18.0//conf/nginx.conf syntax is oknginx: 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://HdhCmsTestyygg测试数据" gt;gt;/app/nginx/html/www/index.html[root@nginx-01 conf]# echo "http://bbs.yygg测试数据" gt;gt;/app/nginx/html/bbs/index.html[root@nginx-01 conf]# echo "192.168.1.5 HdhCmsTestyygg测试数据 bbs.yygg测试数据" gt;gt;/etc/hosts
启动服务并测试它
[root@nginx-01 conf]# /app/nginx/sbin/nginx[root@nginx-01 conf]# curl HdhCmsTestyygg测试数据http://HdhCmsTestyygg测试数据[root@nginx-01 conf]# curl bbs.yygg测试数据http://bbs.yygg测试数据
2. 虚拟主机 别名设置
以www站点为例,设置别名 所谓别名,就是在主域名之外设置一个或多个域名。
为HdhCmsTestyygg测试数据设置别名yygg测试数据。
[root@nginx-01 conf]# cat extra/HdhCmsTestconf server { listen 80; server_name HdhCmsTestyygg测试数据 yygg测试数据; 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/hosts192.168.1.5 HdhCmsTestyygg测试数据 bbs.yygg测试数据 yygg测试数据[root@nginx-01 conf]# curl yygg测试数据http://HdhCmsTestyygg测试数据
3.Nginx status状态信息配置
状态记录由“ngx _ http _ stub _ status _ module”模块实现。
输入/app/nginx/sbin/nginx -V,检查编译中是否存在上述模块:
nginx version: nginx/1.18.0built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017TLS SNI support enabledconfigure arguments: --user=nginx --group=nginx --prefix=/app/nginx-1.18.0/ --with-http_stub_status_module --with-http_ssl_module
创建具有状态的 虚拟主机 ,请参考标题1。status.conf配置文件如下所示:
server { listen 80; server_name status.yygg测试数据; location / { stub_status on; access_log off; } }
主配置文件nginx.conf附加了状态 虚拟主机 配置。
sed -i '11 i include extra/status.conf;' nginx.conf
检查语法并重启nginx
/app/nginx/sbin/nginx -t/app/nginx/sbin/nginx -s reload
配置主机解析
192 168 1 . 5 status.yygg测试数据
去status.yygg测试数据看看
[root@nginx-01 conf]# curl status.yygg测试数据Active connections: 1 server accepts handled requests 4 4 4 Reading: 0 Writing: 1 Waiting: 0
显示结果分辨率:
活动连接数:1 ##正在处理的连接数为1 服务器##已处理4个连接 接受##已创建4个握手 已处理请求##已处理4个请求 读取##读取到客户端的头信息数 写入##返回到客户端的头信息数 等待##NGinx已处理完等待下一个请求的常驻指令数。
4.增加错误日志
Error_log语法:
error_log文件级别;
关键字保持不变,file是日志存储位置,level是错误日志级别。
通常,只使用三个级别的警告|错误|致命一击
nging.conf文件中的错误日志配置worker_processes 1。夏阿德
错误日志日志/错误日志;
没错,就这一行!
以上是Nginx安装后常用功能配置的详细情况。
查看更多关于Nginx安装后常用功能的基本配置的详细内容...