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. ASP.NET Core MVC 打造一个简单的图书馆管理系统 (修正版)(五)外借/阅览图书信息的增删改查

    前言: 本系列文章主要为我之前所学知识的一次微小的实践,以我学校图书馆管理系统为雏形所作. 本系列文章主要参考资料: 微软文档:https://docs.microsoft.com/zh-cn/asp ...

  2. 点击button传递消息,但是页面不跳转的解决方法

    最近在做一个物联网的项目时遇到的问题:界面上有很多控制开/关灯的button,通过点击button来控制各个灯的亮灭.我需要将获取的不同的点击事件消息,以Socket通信的方式发送给硬件端的服务监听程 ...

  3. bzoj 4385: [POI2015]Wilcze doły【单调栈】

    对于每个i,以它为左端点的最优右端点一定是单增的,所以用单调栈维护 具体的,单调栈里放的是和单调的长为d的子段,然后枚举右端点,如果这段的和-当前长为d子段最大的和大于p的话,左端点右移同时注意单调栈 ...

  4. 多选下拉框(select 下拉多选)

    方法一:使用multiple-select.js和multiple-select .css实现 HTML代码: <select id='checkedLevel' style="wid ...

  5. B - Burning Midnight Oil CodeForces - 165B

    One day a highly important task was commissioned to Vasya — writing a program in a night. The progra ...

  6. strings命令的实现 2014-06-02 00:17 355人阅读 评论(0) 收藏

    本程序实现从文件中提取连续4个以上的可打印字符.模仿linux中string命令 #include <stdio.h> #include<stdlib.h> #include ...

  7. android:process用法

    1.作用 android:process将组件在新进程中运行. 2.应用范围 可以出现在<application>  <activity>, <service>,  ...

  8. html下的图片链接有边框的解决方法

    使用dreamweaver创建网页后,上传到网站发现网页的图片链接有非常难看的蓝色边框,而在dw下是没有的 后来查看了一下网上的资料,发现加一个border="0"即可,默认是有边 ...

  9. 转 叫板OpenStack:用Docker实现私有云

    http://www.cnblogs.com/alexkn/p/4239457.html 看到各大厂商的云主机,会不会觉得高大上?目前大公司的主流方案是OpenStack,比如某个公司的私有云

  10. zTree树形控件讲解

    由于截图时间距离有些长,找不到原文出处,如有侵权请联系删除.