一、静态页面配置
创建 conf.d目录
mkdir /usr/local/nginx/conf.d
vim /usr/local/nginx/conf/nginx
user nginx;
include /usr/local/nginx/conf.d/*.conf;
接下来重启nginx
配置当网页访问时的nginx界面
vim /usr/local/nginx/html/index.html
此时配置成想要的页面效果即可
二、虚拟主机配置
1、配置(同端口不同ip)
vim /usr/local/nginx/conf.d/virtualhost.conf
server {
listen 192.168.157.200:80;
server_name localhost;
location / {
root /data/web1/html;
index index.html index.htm;
}
}
server {
listen 192.168.157.201:80;
server_name localhost;
location / {
root /data/web2/html;
index index.html index.htm;
}
}
创建目录 /data/web1/html 和/data/web2/html
以下两种都可以
mkdir /data/web{1,2}/html -p
vim /data/web1/html/index.html
this is web1
vim /data/web2/html/index.html
this is web2
重启nginx
systemctl start nginx
添加IP 192.168.157.200 和 192.168.157.201
2、配置(同ip不同端口)
vim /usr/local/nginx/conf.d/virtualhost.conf
server {
listen 192.168.157.200:80;
server_name localhost;
location / {
root /data/web1/html;
index index.html index.htm;
}
}
server {
listen 192.168.157.200:81;
server_name localhost;
location / {
root /data/web2/html;
index index.html index.htm;
}
}
3、hosts解析域名访问
vim /usr/local/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name web1.xiaoming.cn;
location / {
root /data/web1/html;
index index.html index.htm;
}
}
server {
listen 81;
server_name web2.xiaohua.cn;
location / {
root /data/web2/html;
index index.html index.htm;
}
}
访问端虚拟机配置hosts
vi /etc/hosts
三、日志配置
日志切割
vim /etc/logrotate.d/nginx
/usr/local/nginx/logs/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
if [ -f /usr/local/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
fi
endscript
}
执行
logrotate -f /etc/logrotate.d/nginx
vim /etc/logrotate.d/nginx 注释一行,添加如下一行
此时用浏览器访问IP可以得到访问日志
四、常用功能配置
1、部署文件共享
只写一个server
vim /usr/local/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name web1.xiaoming.cn;
location / {
root /data/web1/html;
index index.html index.htm;
autoindex on;
}
}
cd /data/web1/html
[root@localhost html]# mv index.html{,.bak}
创建几个压缩文件
[root@localhost html]# touch wget-1.{1..4}.tar.gz
接下来重启nginx
(这样在网页上就可以下载)
2、状态模块
vim /usr/local/nginx/conf.d/virtualhost.conf
server {
listen 80;
server_name web1.xiaoming.cn;
location / {
root /data/web1/html;
index index.html index.htm;
autoindex on;
}
location /nginx_status {
stub_status on;
access_log off;
}
}
重启nginx