基于域名的虚拟主机
关闭防火墙
[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
安装nginx
下载好nginx安装包,共享进虚拟机
[root@localhost ~]yum -y install gcc gcc-c++ make zlib-devel pcre pcre-devel elinks lynx
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar zxvf nginx-1.12.0.tar.gz
[root@localhost ~]# cd nginx-1.12.0/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.12.2]#make &&make install
[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@localhost ~]# nginx
[root@localhost ~]# lynx 127.0.0.1 ##可以不用客户端,本地测试一下
优化nginx启动
使用systemctl开启
[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forcking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl enable nginx.service
备份配置文件
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp -p nginx.conf nginx.conf.bak
创建软链接,方便修改配置文件
[root@localhost ~]# ln -s /usr/local/nginx/conf/nginx.conf /etc/
[root@localhost ~]# ls -l /etc/nginx.conf
lrwxrwxrwx. 1 root root 32 8月 8 15:23 /etc/nginx.conf -> /usr/local/nginx/conf/nginx.conf
基于域名的虚拟主机
DNS配置省略
创建站点目录,编写站点首页文件
[root@localhost named]# mkdir -p /var/www/njit
[root@localhost named]# mkdir -p /var/www/hkzx/
[root@localhost named]# vim /var/www/njit/index.html
<h1> welcome to njit university</h1>
[root@localhost named]# vim /var/www/hkzx/index.html
welcome to hkzx hightschool
修改nginx配置文件
[root@localhost ~]# vim /etc/nginx.conf
server {
listen 80;
server_name www.njit.com;
charset utf-8;
access_log logs/www.njit.com.access.log;
location / {
root /var/www/njit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.hkzx.com;
charset utf-8;
access_log logs/www.hkzx.com.access.log;
location / {
root /var/www/hkzx;
index index.html index.htm;
}
#location /status {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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@localhost named]# systemctl restart nginx
[root@localhost named]# netstat -antp | grep nginx ###重启后一定看看进程有没有开启,没有的话再start
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 11825/nginx: master
客户端访问
基于ip地址的虚拟主机
可以给服务器在添加一张网卡
把配置文件中listen 80 改成listen 20.0.0.110:80 即可
[root@localhost ~]# vim /etc/nginx.conf
server {
listen 20.0.0.110:80; ###
server_name www.njit.com;
charset utf-8;
access_log logs/www.njit.com.access.log;
location / {
root /var/www/njit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 20.0.0.123:80; ###
server_name www.hkzx.com;
charset utf-8;
access_log logs/www.hkzx.com.access.log;
location / {
root /var/www/hkzx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -antp |grep nginx
tcp 0 0 20.0.0.123:80 0.0.0.0:* LISTEN 13356/nginx: master
tcp 0 0 20.0.0.110:80 0.0.0.0:* LISTEN 13356/nginx: master
客户端访问
基于端口的虚拟主机
修改配置文件使用同一ip地址不同的端口即可
[root@localhost ~]# vim /etc/nginx.conf
server {
listen 20.0.0.110:80; ###
server_name www.njit.com;
charset utf-8;
access_log logs/www.njit.com.access.log;
location / {
root /var/www/njit;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 20.0.0.110:8080; ###
server_name www.hkzx.com;
charset utf-8;
access_log logs/www.hkzx.com.access.log;
location / {
root /var/www/hkzx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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@localhost ~]# systemctl start nginx ##有的时候查看nginx状态不对,使用pikill nginx 在开启即可
[root@localhost ~]# netstat -antp |grep nginx
tcp 0 0 20.0.0.110:8080 0.0.0.0:* LISTEN 13971/nginx: master
tcp 0 0 20.0.0.110:80 0.0.0.0:* LISTEN 13971/nginx: master
客户端访问
客户端控制
[root@localhost ~]# vim /etc/nginx.conf
location / {
deny 20.0.0.85; ###如果访问不了,删除allow all
allow all; ##
root /var/www/hkzx;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# systemctl stop nginx
[root@localhost ~]# netstat -antp |grep nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -antp |grep nginx
tcp 0 0 20.0.0.110:8080 0.0.0.0:* LISTEN 14122/nginx: master
tcp 0 0 20.0.0.110:80 0.0.0.0:* LISTEN 14122/nginx: master
客户端访问
身份验证
创建用户
[root@localhost ~]# yum -y install httpd ##因为需要使用 htpasswd
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db lisa
New password:
Re-type new password:
Adding password for user lisa
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db
[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
修改配置文件
[root@localhost ~]# vim /etc/nginx.conf
server {
auth_basic "secret"; ##
auth_basic_user_file /usr/local/nginx/passwd.db; ##
listen 20.0.0.110:80;
server_name www.njit.com;
charset utf-8;
access_log logs/www.njit.com.access.log;
location / {
root /var/www/njit;
index index.html index.htm;
}
#location /status {
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -antp |grep nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -antp |grep nginx
tcp 0 0 20.0.0.110:8080 0.0.0.0:* LISTEN 14331/nginx: master
tcp 0 0 20.0.0.110:80 0.0.0.0:* LISTEN 14331/nginx: master
客户端测试
站点统计
[root@localhost ~]# vim /etc/nginx.conf
server {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
listen 20.0.0.110:80;
server_name www.njit.com;
charset utf-8;
#access_log logs/www.njit.com.access.log;
location / {
root /var/www/njit;
index index.html index.htm;
}
location /status { ### 加入这一段
stub_status on; ###开启站点统计
access_log off; ###关闭日志
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
[root@localhost ~]# 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@localhost ~]# systemctl stop nginx
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# netstat -antp |grep nginx
tcp 0 0 20.0.0.110:8080 0.0.0.0:* LISTEN 14421/nginx: master
tcp 0 0 20.0.0.110:80 0.0.0.0:* LISTEN 14421/nginx: master
客户端测试
总结
- 在修改nginx的配置文件的时候记得备份,里面的{}很容易错
- 修改配置文件后重启,要记得查看下连接和端口状态,如果没有再开再起
- 验证结果的时候,删除浏览器的记录再验证