linux(centos)下nginx虚拟主机的配置

来源:互联网 发布:马自达 mx5 rf 知乎 编辑:程序博客网 时间:2024/05/16 10:26

在看下面的内容之前,如果你的nginx还没有安装,可以参照这篇文章——nginx的安装来完成nginx的安装,我的Nginx的安装目录是/usr/local/nginx。下面就开始说如何配置nginx虚拟主机了,注意:在修改配置文件之前,你最好将它备份一下。
1.打开hosts文件

vim /etc/hosts

2.在文本内容的最后增加一行,其中的域名是自定义的。

127.0.0.1   www.test.com

3.我们在/var/www下新建一个目录test,并且在test目录中新建一个文件index.html,在里面写上一些内容。

    cd /var/www    mkdir test    cd test    vim index.html

4.打开nginx的配置文件,按照文件中已有代码的格式添加一个server,我们暂时只修改下面标注的两处即可,如下:

vim /user/local/nginx/conf/nginx.conf
server {        listen       80;#下面的server_name后面填写在hosts中设置的域名        server_name www.test.com;        #charset koi8-r;        #access_log  logs/host.access.log  main;        location / {#下面root后面设置的是网站的根路径,也就是你的项目文件夹的路径            root   /var/www/test;            index  index.html index.htm;        }        #error_page  404              /404.html;        # redirect server error pages to the static page /50x.html        #        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        # proxy the PHP scripts to Apache listening on 127.0.0.1:80        #        #location ~ \.php$ {        #    proxy_pass   http://127.0.0.1;        #}        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000        #        #location ~ \.php$ {        #    root           html;        #    fastcgi_pass   127.0.0.1:9000;        #    fastcgi_index  index.php;        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;        #    include        fastcgi_params;        #}        # deny access to .htaccess files, if Apache's document root        # concurs with nginx's one        #        #location ~ /\.ht {        #    deny  all;        #}    }

5.重启nginx。

/usr/local/nginx/sbin/nginx -s reload

6.浏览器中输入www.test.com即可看到你在index.html中写的内容

1 0
原创粉丝点击