给每个站点分别创建一个 nginx 配置文件 example1.com.conf。
大家好,今天小编关注到一个比较有意思的话题,就是关于nginx实现虚拟主机的问题,于是小编就整理了3个相关介绍nginx实现虚拟主机的解答,让我们一起看看吧。

文章目录:
这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,族高假设:
IP地址: 202.55.1.100
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2
配置 nginx virtualhosting的基本思路和步骤如下:
把2个站点 example1.com, example2.com 放到 nginx 可以访问的目录 /www/
给每个站点分别创建一个氏没 nginx 配置文件 example1.com.conf,example2.com.conf, 并把配置文件放到 /etc/nginx/vhosts/
然后在 /etc/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)
重启 nginx
具体过程
下面是具体的配置过程:
1、在 /etc/nginx 下创建歼穗纳 vhosts 目录
mkdir /etc/nginx/vhosts
2、在 /etc/nginx/vhosts/ 里创建一个名字为 example1.com.conf 的文件,把以下内容拷进去
server{
listen 80;
server_name example1.com www. example1.com;
access_log /www/access_ example1.log main;
location / {
root /www/example1.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
3、在 /etc/nginx/vhosts/ 里创建一个名字为 example2.com.conf 的文件,把以下内容拷进去
server {
listen 80;
server_name example2.com www. example2.com;
access_log /www/access_ example1.log main;
location / {
root /www/example2.com;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
4、打开 /etc/nginix.conf 文件,在相应位置加入 include 把以上2个文件包含进来
user nginx;
worker_processes 1;
# main server error log
error_log /var/log/nginx/error.log ;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
# main server config
http{
include mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr – $remote_user [$time_local] $request ‘
‘”$status” $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name _;
access_log /var/log/nginx/access.log main;
server_name_in_redirect off;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
# 包含所有的虚拟主机的配置文件
include /usr/local/etc/nginx/vhosts/*;
}
5、重启 Nginx
/etc/init.d/nginx restart
1、先进入到nginx的配置文件目录,输入以御州滚下命令:
cd /alidata/server/nginx/conf/rewrite
2、再输入:ll
3、输入以下命令:
cd /alidata/server/nginx/conf/vhosts
4、进入到网站配置目录,打开配置文件,镇余修改好伪静态调用文件;
5、测试配置的文件是否正确,输入:
/alidata/server/nginx/sbin/nginx -t
nginx: the configuration file /alidata/server/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /alidata/server/nginx/conf/nginx.conf test is successful
如果出现以上两句话就说明配置成功了。
6、下面重启下nginx
/alidata/server/nginx/sbin/nginx -s reload
7、伪静态配置迹迅完成
一般Nginx的配置都默认在 /usr/local/etc/nginx/nginx.conf 里,但是如果我们有多个虚拟主机需要配置的话,全部写在 nginx.conf 下会很臃肿,因此呢, Nginx 提供 include 包含其他配置文件的袜蚂明方式帮助我们解决这个问题,接下来我们就来看看具体怎么配置
安装好 Nginx 后,其默认配置文件不需要更改,它已经帮我们导入了其他配置文件的目录,如上图,最后一句 include servers/*; ,意思就是该配置文件包含 nginx.conf 的同级目录下的 servers 文件夹下所有的文件,这时候我们只需要在 nginx.conf 的同告告级目录下创建一个 servers 的文件夹,然后创建自己的 .conf 文件就行,当然物蔽,如果你的配置文件是其他目录,就修改 include 后的路径就可以
xxx.conf 中的只需要写 server 块就行,其实就相当于把这个 server 块添加到 nginx.conf 中的 http 块中
重启 nginx
sudo nginx -s reload
到此,以上就是小编对于nginx实现虚拟主机的问题就介绍到这了,希望介绍关于nginx实现虚拟主机的3点解答对大家有用。
服务器购买/咨询热线:15637009171或15617636856
本文链接:http://43.134.181.216/jishuzhichiyuweihu/29453.html