配置基于域名虛擬機(jī)的nginx.conf 內(nèi)容
在/application/nginx/conf目錄下修改nginx.conf 的內(nèi)容如下(修改配置文件之前習(xí)慣備份)
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;
}
}
}
創(chuàng)建域名對應(yīng)的站點(diǎn)目錄及文件
[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 已經(jīng)啟動。
[root@pdm1-centos6 ~]# /application/nginx/sbin/nginx -s reload
## 啟動Nginx [root@pdm1-centos6 ~]# /application/nginx/sbin/nginx
## 如果沒有啟動Nginx 則無法reload
- 檢查Nginx 重啟的情況,查看端口與進(jìn)程是否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 下測試域名站點(diǎn)配置的訪問結(jié)果:
[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 下測試域名站點(diǎn)配置的訪問結(jié)果:
hosts 解析:在C:\Windows\System32\drivers\etc這個路徑下找到hosts 文件,想辦法在后面追加10.0.0.200 www.。如果有權(quán)限的問題,把文件復(fù)制到桌面,修改好了之后再復(fù)制回來,這個文件是沒有后綴的。如果需要添加多個域名,在后面空格添加就行了。
配置好之后可以在dos 里ping 一下該域名。
C:\Users\Administrator>ping www.
正在 Ping www. [10.0.0.200] 具有 32 字節(jié)的數(shù)據(jù):
來自 10.0.0.200 的回復(fù): 字節(jié)=32 時間=535ms TTL=64
最后在瀏覽器中輸入http://www./ ,能訪問到如下頁面表示一切正常。
配置多個域名
- 配置文件中添加多個server
- http 中添加域名個數(shù)與長度
- 文件路徑同上