nginx虚拟主机配置实践
1、配置基于域名的虚拟主机
[root@web01 html]# egrep -v "#|^$" /application/nginx/conf/nginx.conf.default >/application/nginx/conf/nginx.conf ####将配置文件中的注释和空行删除[root@web01 html]# vim /application/nginx/conf/nginx.conf #####修改如下特殊颜色内容
worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name www.suffergtf.com; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}[root@web01 html]# mkdir /application/nginx/html/www ####创建www站点目录[root@web01 html]# echo "hello,this is a test page for www" >/application/nginx/html/www/index.html ####添加www站点默认首页文件[root@web01 html]# /application/nginx/sbin/nginx -t ###检查配置文件nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful[root@web01 html]# /application/nginx/sbin/nginx -s reload ####重新加载nginx[root@web01 html]# lsof -i :80 ####检查nginx是否正常启动COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEnginx 30396 root 6u IPv4 38676 0t0 TCP *:http (LISTEN)nginx 30399 nginx 6u IPv4 38676 0t0 TCP *:http (LISTEN)[root@web01 html]# echo "192.168.127.11 www.suffergtf.com" >>/etc/hosts[root@web01 html]# tail -1 /etc/hosts192.168.127.11 www.suffergtf.com[root@web01 html]# curl www.suffergtf.comhello,this is a test page for www
2、配置多个基于域名的虚拟主机,并且将虚拟主机配置到单独的配置文件
[root@web01 ~]# cd /application/nginx/conf
[root@web01 conf]# mkdir extra ####创建虚拟主机配置文件目录
[root@web01 conf]# cd extra/[root@web01 extra]# vim www.conf ####编辑www虚拟主机,内容如下
server { listen 80; server_name www.suffergtf.com; location / { root html/www; index index.html index.htm; }}[root@web01 extra]# cp www.conf blog.conf #####直接cp,并且修改blog虚拟主机配置[root@web01 extra]# vim blog.conf
server { listen 80; server_name blog.suffergtf.com; location / { root html/blog; index index.html index.htm; }}[root@web01 extra]# cp www.conf bbs.conf[root@web01 extra]# vim bbs.conf
server { listen 80; server_name bbs.suffergtf.com; location / { root html/bbs; index index.html index.htm; }}[root@web01 extra]# vim ../nginx.conf ####配置nginx主配置文件
worker_processes 1;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf;}[root@web01 extra]# mkdir ../../html/{blog,bbs} [root@web01 extra]# ll ../../html/总用量 20-rw-r--r-- 1 root root 537 6月 5 17:05 50x.htmldrwxr-xr-x 2 root root 4096 6月 5 19:24 bbsdrwxr-xr-x 2 root root 4096 6月 5 19:24 blog-rw-r--r-- 1 root root 612 6月 5 17:05 index.htmldrwxr-xr-x 2 root root 4096 6月 5 18:52 www[root@web01 extra]# echo "hello,this is test for blog" >../../html/blog/index.htm[root@web01 extra]# echo "hello,this is test for bbs" >../../html/bbs/index.htm[root@web01 extra]# ../../sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful[root@web01 extra]# ../../sbin/nginx -s reload[root@web01 extra]# echo "192.168.127.11 blog.suffergtf.com bbs.suffergtf.com" >>/etc/hosts[root@web01 extra]# tail -2 /etc/hosts192.168.127.11 www.suffergtf.com192.168.127.11 blog.suffergtf.com bbs.suffergtf.com[root@web01 extra]# curl www.suffergtf.comhello,this is a test page for www[root@web01 extra]# curl blog.suffergtf.comhello,this is test for blog[root@web01 extra]# curl bbs.suffergtf.comhello,this is test for bbs
3、虚拟主机的别名配置
[root@web01 extra]# vim www.conf
server {
listen ;
server_name suffergtf.com www.suffergtf.com; ####添加域名
location / {
root html/www;
index index.html index.htm;
}
}[root@web01 extra]# ../../sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful[root@web01 extra]# ../../sbin/nginx -s reload[root@web01 extra]# echo "192.168.127.11 suffergtf.com" >>/etc/hosts[root@web01 extra]# curl suffergtf.comhello,this is a test page for www##########别名的用处:1、访问不同的域名,希望访问的是同一虚拟主机;2、集群环境中,所有节点都是同一域名,所以需要通过别名来检测每一个节
4、Nginx状态信息功能
[root@web01 extra]# ../../sbin/nginx -V ####查看编译参数
nginx version: nginx/
built by gcc (Red Hat -) (GCC)
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --prefix=/application/nginx-/ --with-http_stub_status_module --with-http_ssl_module ####需要开启这个信息功能参数
[root@web01 extra]# cat >>/application/nginx/conf/extra/status.conf<<eof ####配置状态信息站点
> server{
> listen ;
> server_name status.suffergtf.com;
> location / {
> stub_status on; ####开启状态信息
> access_log off;
> }
> }
> eof
[root@web01 extra]# vim ../nginx.conf
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
[root@web01 extra]# echo "192.168.127.11 status.suffergtf.com" >>/etc/hosts
[root@web01 extra]# curl status.suffergtf.com
Active connections: #####nginx正在处理的活动连接数
server accepts handled requests ###server表示nginx启动到现在共处理了8个连接;accepts表示从nginx启动到现在处理了8次握手;handled requests表示共处理了8次请求
Reading: Writing: Waiting: 0 ###reading表示nginx读取到客户端的header信息数;writing表示nginx返回给客户端的header信息数;waiting表示nginx已经处理完正在等待下一次请求的驻留连接waiting=active-(reading+writing)
5、nginx错误日志
error_log语法如下
error_log file level;关键字 日志文件 错误日志级别 ####级别【debug|info|notice|warn|error|crit|alert|emerg】默认为error级别错误日志可放置在:main,http,server,location模块;通常放在main中即可[root@web01 extra]# vim ../nginx.conf
worker_processes 1;error_log logs/error.log error; #######开启错误日志功能events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf; include extra/status.conf;}
6、nginx访问日志
nginx访问日志,主要由log_format,access_log控制,默认配置如下 #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;将log_format放在nginx.conf配置文件的http模块,在虚拟主机server中配置access_log[root@web01 extra]# vim ../nginx.conf
worker_processes 1;error_log logs/error.log error;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf; include extra/status.conf;}[root@web01 extra]# vim www.conf
server { listen 80; server_name suffergtf.com www.suffergtf.com; access_log logs/access_www.log main; location / { root html/www; index index.html index.htm; }}[root@web01 extra]# vim blog.conf
server { listen 80; server_name blog.suffergtf.com; access_log logs/access_blog.log main; location / { root html/blog; index index.html index.htm; }}[root@web01 extra]# vim bbs.conf
server { listen 80; server_name bbs.suffergtf.com; access_log logs/access_bbs.log main; location / { root html/bbs; index index.html index.htm; }}[root@web01 extra]# ../../sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful[root@web01 extra]# ../../sbin/nginx -s reload[root@web01 extra]# tail -1 ../../logs/access_www.log [root@web01 extra]# curl www.suffergtf.comhello,this is a test page for www[root@web01 extra]# tail -1 ../../logs/access_www.log 192.168.127.11 - - [05/Jun/2018:22:18:21 +0800] "GET / HTTP/1.1" 200 34 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"#####在高并发可以将access_log增加buffer和flush选项,提升网站性能[root@web01 extra]# vim www.conf
server { listen 80; server_name suffergtf.com www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; }}
7、nginx访问日志轮询切割
[root@web01 extra]# mkdir /server/scripts[root@web01 extra]# vim /server/scripts/www_logrotate.sh ####编辑日志切割脚本
#!/bin/bash######this a logrotate shell for www.suffergtf.com#######Dateformat=$(date +%Y%m%d) ####定义日期,通过日期命名轮询的日志名称Basedir="/application/nginx" ####定义nginx安装目录,方便后面重新加载Nginxlogdir="$Basedir/logs" ####定义日志目录Logname="access_www" ####日志名称[ -d $Nginxlogdir ]&&cd $Nginxlogdir||exit 1 #####如果$Nginxlogdir存在则进入该目录,不存在则退出脚本[ -f ${Logname}.log ] ||exit 1 #####若果${Logname}.log不存在则推出/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log ####改名$Basedir/sbin/nginx -s reload ####重新加载nginx生成新的日志文件 find ${Nginxlogdir} -mtime +60 -name *_${Logname}.log |xargs rm -f ####删除60天以前的日志,日志保留60天
[root@web01 extra]# cat >>/var/spool/cron/root <<eof > ####logrotate for www.suffergtf.com by suffergtf> 00 00 * * * /bin/sh /server/scripts/www_logrotate.sh >/dev/null 2>&1> eof[root@web01 extra]# crontab -l####logrotate for www.suffergtf.com by suffergtf00 00 * * * /bin/sh /server/scripts/www_logrotate.sh >/dev/null 2>&1
8、nginx rewrite
rewrite语法rewrite ^/(.*) http://www.suffergtf.com/$1 permanent;rewrite是关键字,$1是前面括号里的部分(.*),结尾的permanent是永久重定向
- 同域名的rewrite
; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; } } server { listen 80; server_name suffergtf.com; rewrite ^/(.*) http://www.suffergtf.com/$1 permanent; ####suffergtf.com跳转到www.suffergtf.com }
- 不同于名的rewrite,配置用户访问blog.suffergtf.com时跳转到www.suffergtf.com/blog/suffergtf.html
[root@web01 extra]# vim blog.conf
[root@web01 extra]# vim blog.conf
server {
listen 80;
server_name blog.suffergtf.com;
access_log logs/access_blog.log main gzip buffer=32k flush=5s;
location / {
root html/blog;
index index.html index.htm;
}
if ($http_host ~* "^(.*)\.suffergtf\.com$"){
set $domain $1;
rewrite ^(.*) http://www.suffergtf.com/$domain/suffergtf.html break;
}
}
[root@web01 extra]# echo "this a test page for blog.suffergtf.com rewrite to www.suffergtf.com/blog/suffergtf.html">../../html/www/blog/suffergtf.html
[root@web01 extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[root@web01 extra]# ../../sbin/nginx -s reload
9、nginx访问认证
nginx认证:auth_basic,auth_basic_user_file[root@web01 extra]# vim www.conf
server { listen 80; server_name www.suffergtf.com; access_log logs/access_www.log main gzip buffer=32k flush=5s; location / { root html/www; index index.html index.htm; auth_basic "auth test"; ####认证提示信息 auth_basic_user_file /application/nginx/conf/htpasswd; }}server { listen 80; server_name suffergtf.com; rewrite ^/(.*) http://www.suffergtf.com/$1 permanent;}[root@web01 extra]# which htpasswd ###这里使用apache的htpasswd,这里如果没有安装请自行安装/usr/bin/htpasswd[root@web01 extra]# htpasswd -bc /application/nginx/conf/htpasswd suffergtf passwordAdding password for user suffergtf[root@web01 extra]# chmod 400 /application/nginx/conf/htpasswd [root@web01 extra]# chown nginx.nginx /application/nginx/conf/htpasswd [root@web01 extra]# ../../sbin/nginx -tnginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is oknginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful[root@web01 extra]# ../../sbin/nginx -s reload

nginx虚拟主机配置实践的更多相关文章
- nginx虚拟主机配置
nginx虚拟主机配置 虚拟主机的概念虚拟主机,就是把一台物理服务器划分成多个"虚拟"的服务器,每一个虚拟主机都可以有独立的域名和独立的目录nginx虚拟主机的配置nginx的 ...
- Nginx高性能服务器安装、配置、运维 (5) —— Nginx虚拟主机配置
六.Nginx虚拟主机配置 建立基于域名的虚拟主机: (1)建立基于域名的虚拟主机配置文件(以abc.com为例): (2)更改虚拟主机配置文件: 更改配置如下(更改部分即可): server { l ...
- Nginx教程(二) Nginx虚拟主机配置
Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...
- Nginx网络架构实战学习笔记(一):Nginx简介、安装、信号控制、nginx虚拟主机配置、日志管理、location 语法、Rewrite语法详解
文章目录 nginx简介 nginx安装 nginx信号控制 nginx虚拟主机配置 日志管理 location 语法 精准匹配的一般匹配 正则匹配 总结 Rewrite语法详解 nginx简介 Ng ...
- Nginx教程(二) Nginx虚拟主机配置 (转)
Nginx教程(二) Nginx虚拟主机配置 1 虚拟主机管理 1.1 Nginx管理虚拟主机 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主 ...
- Nginx虚拟主机配置(20200202)
一台机器上跑多个站点,即多个域名 curl -xIP:port 域名 用来指定访问的域名在哪个IP的哪个端口上 Nginx默认虚拟主机 不管什么域名解析到该服务器,都会访问到默认虚拟主机 ngi ...
- Nginx虚拟主机配置教程
说明:配置之前先把域名解析到服务器IP地址上 站点1:bbs.osyunwei.com 程序所在目录/data/osyunwei/bbs 站点2:sns.osyunwei.com 程序所在目录/d ...
- Nginx虚拟主机配置--配置Nginx的主配置文件
单台Nginx WEB服务器同时会配置N个网站,也可称之为配置N个虚拟域名的主机,即多个域名对应同一个80端 口. 每个虚拟主机可以是一个独立网站.可以具有独立域名,同一台物理机上面的虚拟主机相互之间 ...
- nginx 虚拟主机配置
user nginx; #代表使用的用户 worker_processes auto; #工作衍生进程数,一般代表系统cpu核数一到两倍最好 error_log /var/log/nginx/erro ...
随机推荐
- 【WIP】Ruby JSON
创建: 2018/03/22 以后有空补上 注: JSON.generate 参数只能是Obejct或者Array, 不可以是Hash https://docs.ruby-lang.org/ja/la ...
- [POI2007]四进制的天平Wag
Description Mary准备举办一个聚会,她准备邀请很多的人参加她的聚会.并且她准备给每位来宾准备一些金子作为礼物.为了不伤及每个人的脸面,每个人获得的金子必须相同.Mary将要用一个天平来称 ...
- js和 php 介绍
转 1. 在公司项目的改造当中,经常会遇到js与php的函数互调的情况,而实际上JS与php的设计者是不提倡这两种语言直接进行调用的,一个是客户端语言,一个服务端语言,两者之间的交互往往靠的是ajax ...
- MVP架构模式
概念解释 MVP是Model(数据) View(界面) Presenter(表现层)的缩写,它是MVC架构的变种,强调Model和View的最大化解耦和单一职责原则 Model:负责数据的来源和封装, ...
- ASP.NET网站发布到服务器
我们一个项目做好了之后想要上线,首先得发布,然后在上传到服务器. 所用到的工具:vs2013(其它vs版本也可以,大致上是一样的) FTP破解版下载链接:http://files.cnblogs.co ...
- 【开源】基于EF6+MVC5+API2+Easyui1.4.5+Easyui管理模板开发的管理系统
经过近一步完善调整,现将本系统源码正式开放,定名为:EasyuiAdminFramework,另外EasyuiAdminTemplate及EasyuiFlatTheme也一并开源 项目主页:http: ...
- hihocoder offer收割编程练习赛11 B 物品价值
思路: 状态压缩 + dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- [BZOJ2809][Apio2012]dispatching 贪心+可并堆
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 我们考虑以每一个节点作为管理者所得的最优答案,一定是优先选择所要薪水少的忍者.那么首 ...
- 在Windows7下编译调试C#程序
要在 命令行下编译C#代码,要配置一下 1.在环境变量下新建一个变量 参数名: csc 参数值:C:\Windows\Microsoft.NET\Framework\v4.0.30319 2.在系统变 ...
- qt查找框设置
转载请注明出处:http://www.cnblogs.com/dachen408/p/7229129.html 主界面弹出查找框方法,查找框显示在主界面上层,并还可以点击主界面,非模态. class ...