1.配置文件与解释

#user  nobody;
worker_processes 1; # 设置工作子进程,默认是1个工作子进程,可以修改,一般设置为CPU的总核数 #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024; # 设置一个工作子进程最大允许多少个连接
} 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 logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server { # 虚拟主机段
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#}
}

2.Nginx虚拟主机配置

  1.基于域名配置(使用的比较多)

    1) 在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

     # 基于域名的虚拟主机配置
server {
listen 80;
server_name www.xbq.com;
location / {
root html/host; # 相对路径,在 nginx/html/host目录中
index admin.html;
}
}

  2) 在nginx/html文件夹中新建 host 文件夹,然后在 host文件夹中 新建admin.html文件,admin.html文件中的内容为:

    Hello,This is host page,www.xbq.com.

  3) 重新加载nginx.conf文件,./nginx -s reload

  4) 修改C:\Windows\System32\drivers\etc\hosts文件,添加如下内容,为了将域名解析:

   

  5) 浏览器访问:www.xbq.com,发现和刚刚写的admin.html内容一样,成功!

     

当访问www.xbq.com的时候,会匹配server中 server_name,然后找到html/host文件夹中的admin.html,返回界面。

  2.基于端口配置

   1) 在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

     # 基于端口号的虚拟主机配置
server {
listen 8888;
server_name test; # 无实际意义,可省略
location / {
root html/port; # 相对路径,在nginx/html/port目录中
index admin.html;
}
}

      2) 在nginx/html文件夹中新建 port文件夹,然后在 port文件夹中 新建admin.html文件,admin.html文件中的内容为:

    Hello,This is port page.

    3) 重新加载nginx.conf文件,./nginx -s reload

    4) 浏览器访问:http://192.168.80.128:8888/,出现如下,则成功:

  3.基于IP配置(使用的比较少)

   (1) 先查看本机的IP,ifconfig

    (2) 添加虚拟网卡

ifconfig eth0:1 192.168.80.150 broadcast 192.168.80.255 netmask 255.255.255.0 up

    route add -host 192.168.80.150 dev eth0:1

    (3) 检测网络是否通:ping 192.169.80.150

  (4)  在nginx/conf/nginx.conf文件中的http段中添加一个server,如下所示:

     # 基于IP的虚拟主机配置
server {
listen 80;
server_name 192.168.80.150; # 新建的虚拟网卡,是内网IP,只能通过 wget访问
location / {
root html/ip;
index admin.html;
}
}

  (5) 在nginx/html文件夹中新建 ip文件夹,然后在 ip文件夹中 新建admin.html文件,admin.html文件中的内容为:

    Hello,This is IP page.

  (6)  重新加载nginx.conf文件,./nginx -s reload

  (7) 到nginx/conf 目录下,访问 刚刚的IP地址:wget  192.168.80.150,发现下载成功!

     

Nginx(二)-- 配置文件之虚拟主机配置的更多相关文章

  1. [原]生产环境下的nginx.conf配置文件(多虚拟主机)

    [原]生产环境下的nginx.conf配置文件(多虚拟主机) 2013-12-27阅读110 评论0 我的生产环境下的nginx.conf配置文件,做了虚拟主机设置的,大家可以根据需求更改,下载即可在 ...

  2. nginx基于域名的虚拟主机配置(本地分布式项目域名配置及测试方法)

    最有用的虚拟主机配置方式. 一个域名只能绑定一个ip地址,一个ip地址可以被多个域名绑定. 可以修改host文件实现域名访问. 前提:即使我们在nginx中配置基于域名的虚拟主机,也需要域名解析,即n ...

  3. nginx 的三种虚拟主机配置方法

    nginx三种虚拟主机配置的方法. 基于端口 在生产环境中一般使用端口或者域名. [root@web01 /etc/nginx/conf.d]# cat web01.conf server { lis ...

  4. Nginx 的全局和虚拟主机配置

    Httpd.conf nginx.conf my-heavy-innode-4G.cnf php.ini  用中文注释 # user:指定 Nginx Worker 进程运行用户和用户组,默认 nob ...

  5. Nginx总结(四)基于域名的虚拟主机配置

    前面讲了如何安装配置Nginx,大家可以去这里看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html 今天要说的 ...

  6. 第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置

    第四百零二节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署,uwsgi安装和启动,nginx的安装与启动,uwsgi与nginx的配置文件+虚拟主机配置 软件版本  uwsgi- ...

  7. Nginx教程(二) Nginx虚拟主机配置

    Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...

  8. Nginx教程(二) Nginx虚拟主机配置 (转)

    Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...

  9. 【nginx运维基础(2)】Nginx的配置文件说明及虚拟主机配置示例

    配置文件说明 #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为当前主机的CPU总核心数. worker_processes 8; #全局错误日志定义类型, ...

随机推荐

  1. less css框架的学习

    什么是LESSCSS LESSCSS是一种动态样式语言,属于CSS预处理语言的一种,它使用类似CSS的语法,为CSS的赋予了动态语言的特性,如变量.继承.运算.函数等,更方便CSS的编写和维护. LE ...

  2. [转]好用工具:Oracle SQL Developer

    我們一直以來就很少使用 Oracle 資料庫,一年下來也頂多 1 ~ 2 個案子採用 Oracle 的資料庫,所以一直都對 Oracle 資料庫的操作不太熟悉,尤其是用 Oracle 內建的那些超難用 ...

  3. VBA学习笔记(2)--新建word文档并插入文字

    说明(2017.3.20): 1. Dim As声明变量类型,Set赋值/初始化 2. With使后面的省略对象,直接点就行,后面要End With 3. Application.StatusBar ...

  4. web编码

    1各种编码 A .1 html编码  -HTML标签 this.Response.Write(this.Server.HtmlEncode("<h1>的作用将文本设置为标题样式! ...

  5. Testng 的数据源 驱动測试 代码与配置

    JUnit中有讲述使用注解的方式进行数据源读取进行自己主动循环測试的方法,在TestNG中也提供了对应的方法 public class TestngDataProvider { /** * 数组内的每 ...

  6. linux实现防止恶意扫描 PortSentry

    linux实现防止恶意扫描 PortSentry   脚本 open 摘要: 端口做为服务器的大门安全很重要,当服务器运行很多服务时并向外提供服务,为防止有人恶意侦测服务器用途,可使用portsent ...

  7. js学习笔记32----new

    new:用于创建一个对象. 有 new 与 无 new 时的区别,查看下面的示例代码应该会增加感觉: <!DOCTYPE html> <html lang="en" ...

  8. js学习笔记16----父节点的操作

    1.元素.parentNode : 只读属性,获取当前元素的父节点. 2.元素.offsetParent : 只读属性,获取离当前元素最近的一个有定位属性(position为relative或者abs ...

  9. [android] AndroidManifest.xml 详解

    第1部分 标签库+包路径+版本控制 <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...

  10. 【转】Hibernate系列学习之(二) 多对一、一对一、一对多、多对多的配置方法

    hihernate一对多关联映射(单向Classes----->Student) 一对多关联映射利用了多对一关联映射原理 多对一关联映射:在多的一端加入一个外键指向一的一端,它维护的关系是多指向 ...