nginx(虚拟主机配置、ip、域名、端口号)

转自:https://blog.csdn.net/www1056481167/article/details/76099787

官网:http://nginx.org/en/docs/http/server_names.html

 

本章主要讲解nginx的虚拟主机。分别是基于域名。基于ip、基于端口号来配置虚拟主机。

大致配置细节在nging.conf的http{}内编写:一下列出简单的配置,详情请看同类文章的nginx(nginx.conf详解)的说明:这里不过多介绍。主要配置细节:

// 全局区
worker_processes 1; // 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
Event {
// 一般是配置nginx连接的特性
// 如1个word能同时允许多少连接
 worker_connections  1024; // 这是指 一个子进程最大允许连1024个连接
}
http {  //这是配置http服务器的主要段
     Server1 { // 这是虚拟主机段       
            Location {  //定位,把特殊的路径或文件再次定位 ,如image目录单独处理
            }             /// 如.php单独处理
     }
     Server2 {
     }
}
server{}:配置虚拟主机必须有这个段。

server_name:虚拟主机的域名,可以写多个域名,类似于别名,比如说你可以配置成 server_name b.ttlsa.com c.ttlsa.com d.ttlsa.com,这样的话,访问任何一个域名,内容都是一样的 listen 80,监听ip和端口,这边仅仅只有端口,表示当前服务器所有ip的80端口,如果只想监听127.0.0.1的80,写法如下: listen 127.0.0.1:80。

root /data/site/b.ttlsa.com:站点根目录,你网站文件存放的地方。注:站点目录和域名尽量一样,养成一个好习惯。

access_log/data/logs/nginx/b.ttlsa.com-access.log main:访问日志 location /{} 默认uri。

基于域名的配置
配置服务器:修改nginx.config的http的server

1、编辑nginx的核心配置文件nginx.conf

vim /usr/local/nginx/sbin/nginx.config
2、添加一个server

server {
    listen    80;
    server_name    z.com;
    location / {
         root z.com;
         index index.html;
    }
 }
3、在相对目录下创建文件和文件夹

Mkdir  z.com(注意是在/usr/local/nginx/下面创建)
Mkdir    z.com/index.html(index.html里面可以随便一些访问的内容)
4、 然后配置绑定ip到域名上:/etc/hosts(当前操作的是linux下的,windows是在C:\Windows\System32\drivers\etc\hosts)

127.0.0.1   localhost
192.168.0.2    z.com#配置域名到ip其他地方不用动(前面的192.168.0.2是本机静态ip)
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
5、然后启动nginx访问网页。

/usr/local/nginx/sbin/nginx -s relaod(软重启)


基于端口
1、  编辑nginx的核心配置文件nginx.conf

/usr/local/nginx/sbin/nginx -s relaod(软重启)
2、添加一个server

server {
    listen    5555;#修改监听的端口是5555
    server_name    z.com;
    location / {
         root z.com;#此处可以使用相对路径也可以使用绝对路径,相对路径是相对于/usr/local/nginx/下面为相对路径,而绝对路径是从/usr/…..为绝对路径
         index index.html;
    }
 }
3、在相对目录下创建文件和文件夹

Mkdir  z.com(注意是在/usr/local/nginx/下面创建)
Mkdir    z.com/index.html(index.html里面可以随便一些访问的内容)
4、然后配置绑定ip到域名上:/etc/hosts

127.0.0.1   localhost
192.168.0.2    z.com#配置域名到ip其他地方不用动
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
5、然后启动nginx访问网页。

/usr/local/nginx/sbin/nginx -s relaod(软重启)


基于ip
1、编辑nginx的核心配置文件nginx.conf

vim /usr/local/nginx/sbin/nginx.config
2、添加一个server

server {
    listen    80;
    server_name    192.168.0.2;#需要使用ifconfig查看本机的ip是多少
    location / {
         root z.com;#此处可以使用相对路径也可以使用绝对路径,相对路径是相对于/usr/local/nginx/下面为相对路径,而绝对路径是从/usr/…..为绝对路径
         index indexIp.html;
    }
 }
3、在相对目录下创建文件和文件夹

Mkdir  z.com(注意是在/usr/local/nginx/下面创建)
Mkdir    z.com/index.html(index.html里面可以随便一些访问的内容)
4、然后配置绑定ip到域名上:/etc/hosts

127.0.0.1   localhost
192.168.0.2    z.com#配置域名到ip其他地方不用动
# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
5、然后启动nginx访问网页。

/usr/local/nginx/sbin/nginx -s relaod(软重启)


 
--------------------- 
作者:Koma-forever 
来源:CSDN 
原文:https://blog.csdn.net/www1056481167/article/details/76099787 
版权声明:本文为博主原创文章,转载请附上博文链接!

你可能感兴趣的:(linux)