详解Nginx 虚拟主机配置的三种方式(基于端口)

 更新时间:2018年10月30日 10:50:34   作者:B8613A  
Nginx配置虚拟主机支持3种方式主要有基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。本篇文章主要介绍了基于端口的实现,感兴趣的小伙伴们可以参考一下

Nginx配置虚拟主机支持3种方式:基于IP的虚拟主机配置,基于端口的虚拟主机配置,基于域名的虚拟主机配置。

详解Nginx 虚拟主机配置的三种方式(基于IP) https://www.jb51.net/article/14974.htm

详解Nginx 虚拟主机配置的三种方式(基于域名) https://www.jb51.net/article/14978.htm

2、Nginx基于端口的虚拟主机配置

如一台服务器只有一个IP或需要通过不同的端口访问不同的虚拟主机,可以使用基于端口的虚拟主机配置。

2.1 假设服务器有个IP地址为192.168.2.154

[root@localhost conf]# ifconfig ens33:4 192.168.2.154/24 up
[root@localhost conf]# ifconfig
ens33:4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
 inet 192.168.2.154 netmask 255.255.255.0 broadcast 192.168.2.255
 ether 00:0c:29:16:90:ae txqueuelen 1000 (Ethernet)

2.2 需要配置的虚拟主机分别为7081、8081和9081,配置主机的host文件便于测试。

[root@localhost conf]# vim /etc/hosts
[root@localhost conf]# cat /etc/hosts|grep 192.168.2.154
192.168.2.154 www.test154.com

2.3 建立虚拟主机存放网页的根目录,并创建首页文件index.html

[root@localhost conf]# cd /data/www/
[root@localhost www]# mkdir port
[root@localhost www]# cd port/
[root@localhost port]# mkdir 7081 8081 9081
[root@localhost port]# ls
7081 8081 9081
[root@localhost port]# echo "port 7081" > 7081/index.html
[root@localhost port]# echo "port 8081" > 8081/index.html
[root@localhost port]# echo "port 9081" > 9081/index.html

2.4 修改nginx.conf,将虚拟主机配置文件包含进主文件

[root@localhost /]# cd /usr/local/nginx/conf/
[root@localhost conf]# ls
fastcgi.conf  fastcgi_params  koi-utf mime.types  nginx.conf  scgi_params  uwsgi_params  win-utf
fastcgi.conf.default fastcgi_params.default koi-win mime.types.default nginx.conf.default scgi_params.default uwsgi_params.default
[root@localhost conf]# vim nginx.conf

在nginx.conf文件末尾加入以下配置

# 在http段中找到以下内容并删除每行前面的“#”
 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
   '$status $body_bytes_sent "$http_referer" '
   '"$http_user_agent" "$http_x_forwarded_for"';

# 配置文件结尾的最后一个“}”之前加入以下语句,如下所示
include vhost/*.conf

2.5 编辑每个端口的配置文件

[root@localhost vhost]# vim www.test154.7081.conf
[root@localhost vhost]# cat www.test154.7081.conf
 server {
 listen 192.168.2.154:7081;
 # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.7081.log main;
 error_log /data/logs/www.test154.7081.error.log;

 location / {
  root /data/www/port/7081;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# vim www.test154.8081.conf
[root@localhost vhost]# cat www.test154.8081.conf
 server {
 listen 192.168.2.154:8081;
 # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.8081.log main;
 error_log /data/logs/www.test154.8081.error.log;

 location / {
  root /data/www/port/8081;
  index index.html index.htm;
 }
 }

[root@localhost vhost]# vim www.test154.9081.conf
[root@localhost vhost]# cat www.test154.9081.conf
 server {
 listen 192.168.2.154:9081;
 # 配置成实际的域名,每个虚拟主机的配置文件域名都相同
 #server_name www.test.com;

 access_log /data/logs/www.test154.9081.log main;
 error_log /data/logs/www.test154.9081.error.log;

 location / {
  root /data/www/port/9081;
  index index.html index.htm;
 }
 }

2.6 创建日志文件,否则无法启动nginx

[root@localhost /]# mkdir -p /data/logs
[root@localhost /]# touch /data/logs/www.test154.7081.log
[root@localhost /]# touch /data/logs/www.test154.7081.error.log
[root@localhost /]# touch /data/logs/www.test154.8081.log
[root@localhost /]# touch /data/logs/www.test154.8081.error.log
[root@localhost /]# touch /data/logs/www.test154.9081.log
[root@localhost /]# touch /data/logs/www.test154.9081.error.log
[root@localhost /]# ls /data/logs/
www.test154.7081.error.log www.test154.8081.error.log www.test154.9081.error.log
www.test154.7081.log www.test154.8081.log www.test154.9081.log

2.7 先测试配置文件然后再启动nginx

[root@localhost /]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 启动nginx
[root@localhost sbin]# ./nginx

2.8 测试文件

[root@localhost ~]# curl http://www.test154.com:7081
port 7081
[root@localhost ~]# curl http://www.test154.com:8081
port 8081
[root@localhost ~]# curl http://www.test154.com:9081
port 9081

附:配置过程中的问题

1、最后测试时发生的问题

[root@localhost sbin]# curl http://www.test154.com:7081
curl: (7) Failed connect to www.test154.com:7081; 拒绝连接
[root@localhost sbin]# curl 192.168.2.154:7081
curl: (7) Failed connect to 192.168.2.154:7081; 拒绝连接

解决方法:

1.1 使用以下命令查看Nginx是否在监听相应的端口

[root@localhost conf]# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address  Foreign Address  State
tcp 0 0 0.0.0.0:111  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.153:80 0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.152:80 0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.151:80 0.0.0.0:*  LISTEN
tcp 0 0 0.0.0.0:8080  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:8081 0.0.0.0:*  LISTEN
tcp 0 0 0.0.0.0:22  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:9081 0.0.0.0:*  LISTEN
tcp 0 0 127.0.0.1:25  0.0.0.0:*  LISTEN
tcp 0 0 192.168.2.154:7081 0.0.0.0:*  LISTEN
tcp6 0 0 :::111   :::*   LISTEN
tcp6 0 0 :::22   :::*   LISTEN
tcp6 0 0 :::23   :::*   LISTEN
tcp6 0 0 ::1:25   :::*   LISTEN

1.2 若Nginx未监听相应端口则重启Nginx服务,再不行重启服务器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Nginx中防止SQL注入攻击的相关配置介绍

    Nginx中防止SQL注入攻击的相关配置介绍

    这篇文章主要介绍了Nginx中防止SQL注入攻击的相关配置介绍,文中提到的基本思路为将过滤的情况用rewrite重订向到404页面,需要的朋友可以参考下
    2016-01-01
  • prometheus监控nginx的实现

    prometheus监控nginx的实现

    这篇文章主要介绍了prometheus监控nginx的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • Nginx方向代理wss或ws的实现示例

    Nginx方向代理wss或ws的实现示例

    本文主要介绍了Nginx方向代理wss或ws的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-10-10
  • Nginx跨域使用字体文件的配置方法

    Nginx跨域使用字体文件的配置方法

    这篇文章主要介绍了Nginx跨域使用字体文件的配置方法,使用HttpHeadersModule模块实现,需要的朋友可以参考下
    2014-06-06
  • Nginx中upstream模块的具体用法

    Nginx中upstream模块的具体用法

    本文主要介绍了Nginx中upstream模块的具体用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • nginx 配置指令之location使用详解

    nginx 配置指令之location使用详解

    这篇文章主要介绍了nginx 配置指令之location使用,Nginx最大的安全问题并不是其安全漏洞,而是在运维上面,尤其是错误配置导致的问题,对nginx location使用感兴趣的朋友一起看看吧
    2022-05-05
  • Nginx实现浏览器可实时查看访问日志的步骤详解

    Nginx实现浏览器可实时查看访问日志的步骤详解

    我们经常需要在页面上实时查看nginx的日志输出,并且能在页面上显示,那么下面小编就给大家说下怎么在浏览器上实时动态的查看nginx的访问日志,有需要的朋友们可以参考借鉴。
    2016-09-09
  • windows下快速安装nginx并配置开机自启动的方法

    windows下快速安装nginx并配置开机自启动的方法

    这篇文章主要介绍了windows下快速安装nginx 并配置开机自启动的相关知识,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-05-05
  • nginx对http请求处理的各个阶段详析

    nginx对http请求处理的各个阶段详析

    这篇文章主要给大家介绍了关于nginx对http请求处理的各个阶段分析的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Nginx服务器限制IP访问的各种情况全解析

    Nginx服务器限制IP访问的各种情况全解析

    这篇文章主要介绍了Nginx服务器限制IP访问的各种情况全解析,包括限制同一IP在一段时间内的访问次数和全局限IP访问以及限制IP访问指定目录等情况,需要的朋友可以参考下
    2015-08-08

最新评论