配置基于域名虚拟机的nginx.conf 内容
在/application/nginx/conf目录下修改nginx.conf 的内容如下(修改配置文件之前习惯备份)
worker_processes 1;
events {
worker_connections 1024;
}
http {
server_names_hash_bucket_size 128;
include mime.types;
default_type application/octet-stream;
sendfile on;
}
server {
listen 80;
server_name www.;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@pdm1-centos6 ~]# mkdir /application/nginx/html/www -p
[root@pdm1-centos6 ~]# echo "http://www." >/application/nginx/html/www/index.html
[root@pdm1-centos6 ~]# cat /application/nginx/html/www/index.html
http://www.
检查语法并重新加载Nginx
- 先检查Nginx 配置文件语法是否有错误:
[root@pdm1-centos6 ~]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
- 重启Nginx,前提是Nginx 已经启动。
[root@pdm1-centos6 ~]# /application/nginx/sbin/nginx -s reload
## 启动Nginx [root@pdm1-centos6 ~]# /application/nginx/sbin/nginx
## 如果没有启动Nginx 则无法reload
- 检查Nginx 重启的情况,查看端口与进程是否OK
[root@pdm1-centos6 ~]# ps -ef|grep nginx
root 1162 1 0 10:09 ? 00:00:00 nginx: master process ../sbin/nginx
nginx 1216 1162 0 10:39 ? 00:00:00 nginx: worker process
root 1225 1128 0 10:43 pts/0 00:00:00 grep nginx
[root@pdm1-centos6 ~]# netstat -lntp|grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1162/nginx
- Linux 下测试域名站点配置的访问结果:
[root@pdm1-centos6 ~]# echo "10.0.0.200 www." >>/etc/hosts
[root@pdm1-centos6 ~]# tail -l /etc/hosts
10.0.0.200 www.
[root@pdm1-centos6 ~]# curl www.
http://www.
提示:不要忘记在访问的客户端做hosts 解析,Linux 或者windows 都需要做hosts 解析。
- windows 下测试域名站点配置的访问结果:
hosts 解析:在C:\Windows\System32\drivers\etc这个路径下找到hosts 文件,想办法在后面追加10.0.0.200 www.。如果有权限的问题,把文件复制到桌面,修改好了之后再复制回来,这个文件是没有后缀的。如果需要添加多个域名,在后面空格添加就行了。
配置好之后可以在dos 里ping 一下该域名。
C:\Users\Administrator>ping www.
正在 Ping www. [10.0.0.200] 具有 32 字节的数据:
来自 10.0.0.200 的回复: 字节=32 时间=535ms TTL=64
最后在浏览器中输入http://www./ ,能访问到如下页面表示一切正常。
配置多个域名
- 配置文件中添加多个server
- http 中添加域名个数与长度
- 文件路径同上