当前位置:   article > 正文

Linux:web服务基于IP和域名部署

Linux:web服务基于IP和域名部署

Linux:web服务基于IP和域名部署

配置基于IP的虚拟主机

# 添加两个IP
[root@server100 ~]# ip addr add 172.25.254.10/24 dev eth0
[root@server100 ~]# ip addr add 172.25.254.20/24 dev eth0

# 这个命令是专门用来查看IP信息的
[root@server100 ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group                                              default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP gro                                             up default qlen 1000
    link/ether 00:0c:29:19:96:ef brd ff:ff:ff:ff:ff:ff
    altname enp3s0
    altname ens160
    inet 172.25.254.100/24 brd 172.25.254.255 scope global noprefixroute                                              eth0
       valid_lft forever preferred_lft forever
    inet 172.25.254.10/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet 172.25.254.20/24 scope global secondary eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::67a0:8915:dfbb:2f8b/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

# 修改主配置文件
[root@server100 conf.d]# vim /etc/nginx/nginx.conf

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
       # root         /usr/share/nginx/html;

        root         /var/www/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location /timinglee/ {
                auth_basic on;
                auth_basic_user_file "/etc/nginx/.htpasswd";

        }

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# 需要改变这一行的位置,需要把它放在server外面,http下面
include /usr/share/nginx/modules/*.conf;

# 创建扩展配置文件配置虚拟主机
[root@server100 conf.d]# vim /etc/nginx/conf.d/vhosts.conf

# 配置内容如下
server {
        listen 172.25.254.10:80;
        root /usr/share/nginx/virtual/10;
        index index.html;
}

server {
        listen 172.25.254.20:80;
        root /usr/share/nginx/virtual/20;
        index index.html;
}

# 创建对应目录以及创建网页文件
[root@server100 conf.d]# mkdir -p /usr/share/nginx/virtual/{10,20}
[root@server100 conf.d]# echo "10" > /usr/share/nginx/virtual/10/index.html
[root@server100 conf.d]# echo "20" > /usr/share/nginx/virtual/20/index.html

# 访问测试
[root@server100 conf.d]# curl 172.25.254.10
10
[root@server100 conf.d]# curl 172.25.254.20
20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83

配置基于域名的虚拟主机(最常用)

# 配置扩展配置文件
[root@server100 conf.d]# vim /etc/nginx/conf.d/vhosts.conf
server {
        listen 172.25.254.10:80;
        root /usr/share/nginx/virtual/10;
        index index.html;
}

server {
        listen 172.25.254.20:80;
        root /usr/share/nginx/virtual/20;
        index index.html;
}

# 主要是下面这两段
server{
        listen 80;
        server_name news.shanxin.org;
        root /usr/share/nginx/virtual/shanxin/news;
        index   index.html;
}

server{
        listen 80;
        server_name bbs.shanxin.org;
        root /usr/share/nginx/virtual/shanxin/bbs;
        index   index.html;
}

# 创建对应目录以及创建网页文件
[root@server100 conf.d]# mkdir -p /usr/share/nginx/virtual/shanxin/{news,bbs}
[root@server100 conf.d]# echo "news" > /usr/share/nginx/virtual/shanxin/news/index.html
[root@server100 conf.d]# echo "bbs" > /usr/share/nginx/virtual/shanxin/bbs/index.html

# 添加Linux系统的本机域名映射文件
[root@server100 conf.d]# vim /etc/hosts

172.25.254.100 server100.timeinglee.org www.shanxin.org news.shanxin.org bbs.shanxin.org

# 访问测试
# 访问测试
[root@server100 conf.d]# curl news.shanxin.org
news
[root@server100 conf.d]# curl bbs.shanxin.org
bbs
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

查看nginx错误日志信息,排查错误 还有通过日志

[root@server100 conf.d]# cat /var/log/nginx/error.log

[root@server100 ~]# tree /var/log/nginx/
/var/log/nginx/
├── access.log # 通过日志
└── error.log

0 directories, 2 files
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/508271
推荐阅读
相关标签
  

闽ICP备14008679号