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 ...
随机推荐
- 关于Struts2上传文件的最大Size的设置
今天使用Struts2的文件上传控件时,在struts.xml中,将处理上传的action中的fileUpload拦截器的maximumSize参数设置为5000000,上传了一个3M的文件后发现控制 ...
- python file.tell() 在windows下需要注意的地方
顺便记一下,'rba'模式是非法的...
- UML中的图的出现顺序
上接:UML从需求到设计--用例 从开始接触UML到现在对UML逐渐有了更深入的了解.刚开始,对于UML总是感觉UML就是图.一提起UML 就想着这个是画图的东西. 具体这些图都是干什么的.为什么会有 ...
- phpcms V9 改造:输出sql语句
.修改数据库驱动 phpcms/libs/classes/mysql..修改模型 phpcms/libs/classes/model..调用 使用自定义模型类查询完成之后,调用模型类的lastsql( ...
- WebRTC源码分析三:视频处理流程
转自:http://blog.csdn.net/neustar1/article/details/19480863 文本介绍视频的处理流程.图1中显示了两路视频会话视频信号流过程. 图1 视频流程示意 ...
- sql server存儲過程語法
-- 变量的声明,sql里面声明变量时必须在变量前加@符号 DECLARE @I INT -- 变量的赋值,变量赋值时变量前必须加set SET @I = 30 -- 声明多个变量 ...
- Kali Linux 2016.2初体验使用总结
Kali Linux 2016.2初体验使用总结 Kali Linux官方于8月30日发布Kali Linux 2016的第二个版本Kali Linux 2016.2.该版本距离Kali Linux ...
- 模拟 ZOJ 3878 Convert QWERTY to Dvorak
题目传送门 /* 模拟:手敲map一一映射,累! 除了忘记读入字符串不能用gets用getline外还是很顺利的AC了:) */ #include <cstdio> #include &l ...
- Android基础环境搭建
1.安装JDK 从官网下载JDK安装, http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151 ...
- HazelCast 的内存管理原理
As it is said in the recent article "Google: Taming the Long Latency Tail - When More Machines ...