关于nginx配置虚拟主机
前提:我的虚拟主机的外网ip为111.231.226.228(是云服务器哈)
本地测试环境为windows7(修改本地的hosts文件)
步骤:(安装nginx可以看看我文章“linux nginx编译安装”)
1、打开nginx.conf文件。(按照我的安装教程,这个文件在:/usr/local/nginx/conf这个目录下)
在nginx.conf文件中写入:
server {
        listen       80;                             //表示监听80端口
        server_name  www.panchao.com;      //域名
#charset koi8-r;
#access_log logs/host.access.log main;
        location / {
            root   html;                            //root目录
            index  index.html index.htm;   //默认优先读取的文件
        }
使用信号控制重新读取配置文件(我的nginx master 的pid为5160,不懂nginx信号控制的朋友,可以百度一下。)。
kill -HUP 5160
只是这样配置,我们用我们本地的电脑访问www.panchao.com肯定是访问不了的。
那我们应该怎样做才能让www.panchao.com能够访问到我的nginx服务器呢?
答案:配置我们本地的hosts文件(这个文件在我们电脑中的C:\Windows\System32\drivers\etc文件夹下)。
当然,如果你有在线域名的话,可以直接去万网进行配置。
hosts配置如下:
# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
#	127.0.0.1      www.panchao.com
	111.231.226.228      www.panchao.com     #加入这一句就行了
这样我们在浏览器中输入网址:www.panchao.com就能访问我们在云服务器上的nginx了。

当然,我们也可以配置端口。
在nginx.conf中配置以下代码:
 server{
        listen 8080;                                       //表示监听8080端口
        server_name www.panchao.com;    
        location / {
                        root html;                                          //表示根目录为html(相对于nginx安装目录)
                        index panchao.html;       //设置优先读取文件为panchao.html
                }
        }
使用信号控制重新读取配置文件:
kill -HUP 5160
编写panchao.html
[root@VM_16_2_centos html]# vi panchao.html
<html>
this is panchao test!!!
</html>
然后我们在本地hosts文件中配置
# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
#	127.0.0.1      www.panchao.com
	111.231.226.228      www.panchao.com
hosts的配置和上面一样。
我们在浏览器中输入:
得到结果

tips:其实我们配置hosts文件就相当于万网域名映射功能,只是配置hosts文件只对当前配置的电脑有效。
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号