Nginx虚拟主机配置--nginx为编译安装的 域名 ip 端口

本文介绍了Nginx配置文件内容,包括全局参数、http服务等设置,其由全局块、events块、http块组成。还阐述了基于域名、IP、端口的虚拟主机配置方法,如修改配置文件、创建index文件、检查语法错误、重新加载配置文件及测试访问等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

了解nginx的配置文件内容

我的nginx是编译安装的

/etc/nginx/nginx.conf

# 全局参数设置
user  nginx;                  #指定用户
worker_processes  1;          #设置nginx启动进程的数量,一般设置成与逻辑cpu数量相同
error_log  logs/error.log;    #指定错误日志
worker_rlimit_nofile 10240;   #设置一个nginx进程能打开的最大文件数
pid        /var/run/nginx.pid;
events {
    worker_connections  1024; #设置一个进程的最大并发连接数
}

# http 服务相关设置
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"'; 
    access_log  /var/log/nginx/access.log  main;    #设置访问日志的位置和格式 
    sendfile          on; #是否调用sendfile函数输出文件,一般设置为on,若nginx是用来进行磁盘IO负载应用时,可以设置为off,降低系统负载
    gzip              on;      #是否开启gzip压缩,将注释去掉开启 
    keepalive_timeout  65;     #设置长连接的超时时间
# 虚拟服务器的相关设置
server { 
        listen      80;        #设置监听的端口 
        server_name  localhost;        #设置绑定的主机名、域名或ip地址 
#        charset koi8-r;        # 设置编码字符 
        charset utf-8;
        location / { 
            root  /var/www/nginx;           #设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;    #设置默认打开的文档 
        }
        error_page  500 502 503 504  /50x.html; #设置错误信息返回页面 
        location = /50x.html { 
            root  html;        #这里的绝对位置是/usr/local/nginx/html
        }
    }
}

 nginx.conf的组成:nginx.conf一共由三部分组成,分别为:全局块、events块、http块。在http块中又包含http全局块、多个server块。每个server块中又包含server全局块以及多个location块。在统一配置块中嵌套的配置快,各个之间不存在次序关系。

基于域名的虚拟主机

修改配置文件   在http块内添加

        server {
        listen       80;
        server_name  www.ceshi1.com;
        location / {
            root   /var/www/nginx1;
            index  index.html index.htm;
            limit_rate  2k;
                }
            }
        server {
        listen       80;
        server_name  www.ceshi2.com;
        location / {
            root   /var/www/nginx2;
            index  index.html index.htm;
            limit_rate  2k;
                }
            }

 listen       80;                端口80

server_name  www.ceshi1.com;                设置绑定的主机名、域名或ip地址 

location / {
            root   /var/www/nginx1;                设置服务器默认网站的根目录位置,需要手动创建
            index  index.html index.htm;                设置默认打开的文档
            limit_rate  2k;
                }

为域名www.ceshi1.com  的虚拟机,创建index文件

[root@b-2 ~]# mkdir -p /var/www/nginx1

[root@b-2 ~]# vim /var/www/nginx1/index.html

                        hello ceshi1

为域名www.ceshi2.com 的虚拟机,创建index文件

[root@b-2 ~]# mkdir -p /var/www/nginx2

[root@b-2 ~]# vim /var/www/nginx2/index.html

                         hello ceshi2

查看配置文件有无语法错误,重新加载配置文件

[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@b-2 ~]# nginx -s reload

在Windows客户端配置解析

win+r      drivers

找到etc文件夹

记事本打开hosts文件  添加域名解析即可

如果无法保存,详看https://www.jianshu.com/p/7bbb309eae6e

测试访问

基于ip的虚拟主机

我们已经添加过测试文件index.html

我们先添加一个临时ip

[root@localhost ~]# yum -y install net-tools
[root@localhost ~]# ifconfig ens33:1 192.168.126.149/24

 修改配置文件

 检查并重新加载配置文件

[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@b-2 ~]# nginx -s reload

 测试访问

 

基于端口的虚拟主机

vim /etc/nginx/nginx.conf

server {
        listen       80;
        server_name  192.168.126.141;
        location / {
            root   /var/www/nginx1;
            index  index.html index.htm;
            limit_rate  2k;
                }
        }
        server {
        listen       8080;
        server_name  192.168.126.141;
        location / {
            root   /var/www/nginx2;
            index  index.html index.htm;
            limit_rate  2k;
                }
        }
[root@b-2 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@b-2 ~]# nginx -s reload

测试访问

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值