一切为了更好!
为了在使用中更高效简洁,Nginx安装后通常会进行一些常用的配置。
如果虚拟主机很多的话,进行分离看起来会更方便,还可以按功能、业务进行划分,
下面以两个虚拟主机为例。
[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;
}
}
}
mkdir extra
[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;
}
[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;
}
}
[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;
}
}
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
以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;
}
}
[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
状态信息记录使用的是`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.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
/app/nginx/sbin/nginx -t
/app/nginx/sbin/nginx -s reload
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已经处理完正在等候下一次请求的指令的驻留连数
error_log file level;
关键字不变,file是日志存放位置,level是错误日志级别
通常只用warn|error|crit三个级别
nging.conf
文件中worker_processes 1;
下添加error_log logs/error_log;
没错,就这一行!