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虚拟主机配置实践的更多相关文章

  1. nginx虚拟主机配置

    nginx虚拟主机配置   虚拟主机的概念虚拟主机,就是把一台物理服务器划分成多个"虚拟"的服务器,每一个虚拟主机都可以有独立的域名和独立的目录nginx虚拟主机的配置nginx的 ...

  2. Nginx高性能服务器安装、配置、运维 (5) —— Nginx虚拟主机配置

    六.Nginx虚拟主机配置 建立基于域名的虚拟主机: (1)建立基于域名的虚拟主机配置文件(以abc.com为例): (2)更改虚拟主机配置文件: 更改配置如下(更改部分即可): server { l ...

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

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

  4. Nginx网络架构实战学习笔记(一):Nginx简介、安装、信号控制、nginx虚拟主机配置、日志管理、location 语法、Rewrite语法详解

    文章目录 nginx简介 nginx安装 nginx信号控制 nginx虚拟主机配置 日志管理 location 语法 精准匹配的一般匹配 正则匹配 总结 Rewrite语法详解 nginx简介 Ng ...

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

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

  6. Nginx虚拟主机配置(20200202)

    一台机器上跑多个站点,即多个域名 curl -xIP:port 域名    用来指定访问的域名在哪个IP的哪个端口上 Nginx默认虚拟主机 不管什么域名解析到该服务器,都会访问到默认虚拟主机 ngi ...

  7. Nginx虚拟主机配置教程

    说明:配置之前先把域名解析到服务器IP地址上 站点1:bbs.osyunwei.com  程序所在目录/data/osyunwei/bbs 站点2:sns.osyunwei.com  程序所在目录/d ...

  8. Nginx虚拟主机配置--配置Nginx的主配置文件

    单台Nginx WEB服务器同时会配置N个网站,也可称之为配置N个虚拟域名的主机,即多个域名对应同一个80端 口. 每个虚拟主机可以是一个独立网站.可以具有独立域名,同一台物理机上面的虚拟主机相互之间 ...

  9. nginx 虚拟主机配置

    user nginx; #代表使用的用户 worker_processes auto; #工作衍生进程数,一般代表系统cpu核数一到两倍最好 error_log /var/log/nginx/erro ...

随机推荐

  1. bzoj 3308: 九月的咖啡店【最大费用最大流】

    费用流里spfa最后的判断要写成dis[t]>=0而不是dis[t]!=-inf否则会WAWAWA-- #include<iostream> #include<cstdio&g ...

  2. [App Store Connect帮助]七、在 App Store 上发行(2.3)设定价格与销售范围:为您的 App 选择地区

    您可以选择希望您的 App 在 App Store 上可用的地区.默认情况下,所有地区都被选中,但您可以取消选中您不想销售您 App 的地区.新地区或已更改地区的 App Store 会在 24 小时 ...

  3. js 编码详解

    今天在整理 js编码解码方法时,在网上搜资料,发现一篇文章讲的不错,讲解的非常简单明了,于是乎就想转载过来,却发现无法转载到博客园,最后只能卑鄙的摘抄过来.js编码解码就是将一些对URL和数据库敏感的 ...

  4. JDBC中的DriverManager.getConnection(url)中的参数url

    1.Oracle8/8i/9i数据库(thin模式) Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); ...

  5. [Usaco2006 Open]The Climbing Wall 攀岩

    Description One of the most popular attractions at the county fair is the climbing wall. Bessie want ...

  6. 题解报告:hdu 2087 剪花布条(KMP入门)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面 ...

  7. Base64编码与解码 分类: 中文信息处理 2014-11-03 21:58 505人阅读 评论(0) 收藏

    Base64是一种将二进制转为可打印字符的编码方法,主要用于邮件传输.Base64将64个字符(A-Z,a-z,0-9,+,/)作为基本字符集,把所有符号转换为这个字符集中的字符. 编码: 编码每次将 ...

  8. B/S和C/S示意图

    B/S C/S

  9. DateFormat类

    package Format_daqo; import java.util.Date; import java.text.DateFormat; public class DateFormatTest ...

  10. 关于tomcat一些简介

    window下,在tomcat的bin目录下,用cmd输入startup.bat 即可启动tomcat 成功启动Tomcat后,通过访问http://localhost:8080/便可以使用Tomca ...