nginx配置文件 nginx.conf 分为三个段 :main段、events段和http段

main段
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events段:
events {
worker_connections 1024;
}

http 段
webapp服务器相关配置。 虚拟主机在该段配置

nginx虚拟主机配置:

虚拟主机:一台机器上通过一些虚拟的路由配置让用户感觉是多台主机。有基于ip的虚拟主机(我们这里不做演示)、基于端口号的虚拟主机和基于域名的虚拟主机.如下nginx配置文件中默认配置了一个server 这个 server监听80端口

server {
   listen    80;
   server_name localhost;
   #charset koi8-r;
   #access_log logs/host.access.log main;
   location / {
     root  html;
     index index.html index.htm;
   }
}

我们通过 zk03:80 访问时可以访问到 index.html 如下图:
nginx 虚拟主机配置

通过端口配置虚拟主机
通过访问不同的端口号实现不同的路由。访问指定的项目或文件。

我们修改nginx.conf 增加一个 server配置 如下。监听8080 端口默认主页地址为 8080.html

 server {
        listen       8080;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  8080.html 8080.htm;
        }
    }

同时在 html 目录中增加一个 8080.html文件 如下:

[[email protected] nginx]# cd html
[[email protected] html]# vi 8080.html
<html><h1>8080index</h><html>

修改完配置文件后 重新加载配置

[[email protected] nginx]# ./sbin/nginx -s reload

浏览器访问zk03:8080 访问到了 8080.html
nginx 虚拟主机配置

通过域名配置虚拟主机
同样端口的情况下,通过不同的域名访问不同的项目或资源。我们这里配置 bbs.test.comask.test.com
配置方法 vi nginx.conf 增加 两个 server 配置

 server {
        listen       80;
        server_name  bbs.test.com;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  bbs.html bbs.htm;
        }
    }
    server {
        listen       80;
        server_name  ask.test.com;
        #access_log  logs/host.access.log  main;
            location / {
            root   html;
            index  ask.html ask.htm;
        }
    }

html下增加 ask.html 和 bbs.html如下:

[[email protected] html]# ll
总用量 20
-rw-r--r--. 1 root root 537 1月  23 10:11 50x.html
-rw-r--r--. 1 root root  30 1月  23 14:30 8080.html
-rw-r--r--. 1 root root  30 1月  23 14:44 ask.html
-rw-r--r--. 1 root root  30 1月  23 14:44 bbs.html

重新加载配置nginx文件
./sbin/nginx -s reload

本地hosts 文件中增加 配置 host 文件位置:C:\Windows\System32\drivers\etc

192.168.68.137 ask.test.com  bbs.test.com 

浏览器分别访问 ask.test.combbs.test.com 都访问到了各自指定的界面 如下图:
nginx 虚拟主机配置
nginx 虚拟主机配置

相关文章:

  • 2021-05-28
  • 2022-02-15
  • 2022-02-04
猜你喜欢
  • 2021-06-22
  • 2021-05-11
  • 2021-09-09
  • 2021-10-07
  • 2021-10-23
  • 2021-07-27
相关资源
相似解决方案