Nginx 笔记与总结(3)配置虚拟主机
Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reload
停止 Nginx 的另外一种方式:
/usr/local/nginx/sbin/nginx -s stop
重读日志文件的另一种方式,相当于 kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`:
/usr/local/nginx/sbin/nginx -s reopen
测试配置文件是否正确:
/usr/local/nginx/sbin/nginx -t
如果配置正确,则会提示 successful:
虚拟主机的管理
vim /usr/local/nginx/conf/nginx.conf
全局区:

worker_processes 1 表示有 1 个工作的子进程,可以进行修改,但是过大没有意义,因为需要占用更多的 CPU 资源,一般设置为 CPU 数 * 核数
events 区:

一般配置 Nginx 进程与连接的特性。worker_connections 1024 表示 1 个子进程(worker)最多允许 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout ;
keepalive_timeout ;
#gzip on;
server {
listen ;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page /.html;
# redirect server error pages to the static page /50x.html
#
error_page /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:;
# 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 ;
# listen somename:;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 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;
# }
#}
}
配置 http 服务器的主要的段。
http 段中的 server 段:
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;
#}
}
server 就是虚拟主机。
【例1】基于域名的虚拟主机
添加 server 段:
server{
listen 80;
server_name dee.com;
location /{
root dee.com;
index index.html;
}
}

保存退出;
location 可以使用绝对路径,也可以使用相对路径,相对路径是相对于 nginx 的根目录:/usr/local/nginx
在 /usr/local/nginx 目录下创建目录 dee.com,在其下创建 index.html:
[root@localhost ~]# cd /usr/local/nginx/
[root@localhost nginx]# mkdir dee.com
[root@localhost nginx]# vim dee.com/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
Welcome to dee.com
</body>
</head>
</html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
配置 hosts:
192.168.254.100 dee.com
访问 dee.com:

【例2】基于 ip 的虚拟主机
编辑配置文件:
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{
listen ;
server_name 192.168.254.100;
location /{
root ip;
index index.html;
}
}
在 /usr/local/nginx 目录下新建 ip 目录:
[root@localhost nginx]# mkdir ip
[root@localhost nginx]# vim ip/index.html
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
ip test
</body>
</head>
</html>
访问 http://192.168.254.100/

【例3】基于端口的虚拟主机
编辑配置文件(vim 查看行号 :set nu):
vim /usr/local/nginx/conf/nginx.conf
添加 server 段:
server{
listen 2022;
server_name dee.com;
location /{
root /var/www;
index index.html;
}
}
这时配置 location 使用绝对路径;
在 /var 下新建 www 目录,在其下新建 index.html 文件:
[root@localhost nginx]# mkdir /var/www
[root@localhost nginx]# vim /var/www/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>dee.com</title>
<body>
Welcome to dee.com's admin panel
</body>
</head>
</html>
重新读取配置文件:
/usr/local/nginx/sbin/nginx -s reload
访问 http://dee.com:2022/

或者访问 http://192.168.254.100:2022/
附:如果需要启用目录浏览,只需要在 Server 段中加入 autoindex on;
Nginx 笔记与总结(3)配置虚拟主机的更多相关文章
- Nginx学习笔记(二)--- 配置虚拟主机
Linux下安装Nginx https://www.cnblogs.com/dddyyy/p/9780705.html 1.虚拟主机介绍 一台服务器分成多个"独立"的主机,每台虚 ...
- Nginx笔记总结五:Nginx配置虚拟主机
upstream proxy1 { server ; } upstream proxy2 { server ; } server { listen ; server_name www1.dlab.co ...
- nginx 配置虚拟主机
文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...
- Nginx安装及配置虚拟主机
nginx安装部分 依赖环境 yum -y install gcc zlib openssl-devel zlib-devel 1. 下载好下面两个包:nginx-1.8.1.tar.gz pcre- ...
- 快速掌握Nginx(一) —— 安装Nginx和简单配置虚拟主机
Nginx安装和简单配置虚拟主机 1 Nginx简介 Nginx是近几年最火热的http.反向代理服务器,百度阿里等互联网公司也都在使用Nginx,它也可以用作邮件代理服务器.TCP/UDP代理服务器 ...
- Nginx下配置虚拟主机的三种方法
Nginx下,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的 ...
- nginx配置虚拟主机vhost的方法详解
Nginx vhost配置,可实现基于ip.端口号.servername的虚拟主机,同时可避免直接修改主配置文件.在nginx下配置虚拟主机vhost非常方便.这篇文章主要介绍了nginx配置虚拟主机 ...
- Nginx安装、配置虚拟主机、反向代理、负载均衡
1. nginx安装 下载nginx: 官方网站: http://nginx.org/ 使用的版本是1.8.0版本. Nginx提供的源码. 1.1. 要求的安装环境 1.需要安装gcc的环境.y ...
- nginx 配置虚拟主机的三种方法
nginx,一个server标签就是一个虚拟主机. 1.基于域名的虚拟主机,通过域名来区分虚拟主机——应用:外部网站 2.基于端口的虚拟主机,通过端口来区分虚拟主机——应用:公司内部网站,外部网站的管 ...
- nginx配置虚拟主机之不同端口和不同IP地址
配置nginx虚拟主机不同端口和不同ip地址,和上编nginx基于域名配置虚拟主机博文类似,请先参考. zxl.com域名不同端口,配置文件内容如下: 1 2 3 4 5 6 7 8 9 10 11 ...
随机推荐
- Android之Intent深入
Android中的意图包含多种用法,本文主要包括以下内容 显式意图 隐匿意图 要求结果回传的意图 显式意图 :必须指定要激活的组件的完整包名和类名 (应用程序之间耦合在一起) 一般激活自己应用的组件的 ...
- 昨天晚上也弄不清楚是自己密码被盗了还是由于ip冲突
所以还是尽量要相信自己所见到的,今天上午是安卓课程,说实话,昨天晚上都是2:30睡的,现在硬是要把时间待这么晚才回去睡,是因为我想尽快入睡,昨天晚上就是眼睛都有点睁不开了,所以就睡得很快,但是早上也是 ...
- 什么才是程序员的核心竞争力?zz
原文出处: 知乎 姚冬的观点 学习能力,尤其是自学能力,你啥时看到那些有名的程序高手在论坛上问“学习 XX 该看什么书,如何快速学习 XXX,学习 XXX 有什么代码推荐”之类的问题,他们想学什么很快 ...
- WebRTC VideoEngine超详细教程(三)——集成X264编码和ffmpeg解码
转自:http://blog.csdn.net/nonmarking/article/details/47958395 本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine超详细教 ...
- Android种 adb是什么(转)
提到adb.exe,一直关注我们Android系列教程的朋友们应该不会感到陌生,因为无论取得 Root权限或者刷机的时候我们都通过adb直接操作管理Android手机,但是可能大多数对于adb仅仅局限 ...
- HDU 4342History repeat itself 数学
C - History repeat itself Time Limit:1000MS Memory Limit:32768KB Description Tom took the D ...
- CC2540开发板学习笔记(四)——定时器
一.实验内容 分别使用定时器T1和T3使得LED周期性闪烁 二.实验过程 1.定时器T1(查询IRCON来控制) (1)需要调配的寄存器 T1CTL(0XE4) Timer1控制寄存器 BIT3, ...
- nodeAPI--HTTP
HTTP: //超文本协议,是属于TCP上层的协议 http协议构建在请求和响应概念上,node.js中对应http.ServerRequest,http.ServerResponse; 当用户浏 ...
- 移动开单软件 手持PDA开单扫描打印系统开发介绍
具体功能预览--(图示) PDA开单打印扫描采集器主程序: ▲门店使用:接单员销售开单.销售退货或查询相关资料. ▲仓库使用:PDA仓库验收货.发货.仓库盘点 ▲在外业务开单:业务在外面开销售单.销售 ...
- Date类型和Long类型的相互转换
Date类型和Long类型的相互转换: import java.text.SimpleDateFormat; import java.util.Date; public class T { publi ...