nginx配置及虚拟主机

一、http协议介绍

1、网站类型

  • 静态网站
    内容是固定的,任何用户访问看到的内容是一样的
    开发语言: html, jquery, js, div+css
    网页文件: xxxx.html

  • 动态网站
    一段程序代码,根据传递的参数不同返回不同的结果
    开发语言:
    PHP, xxxxx.php
    JAVA, xxxxx.jsp

2、涉及的软件

  • httpd
  • nginx
  • tomcat

3、http协议介绍

http, 明文, 超文本传输协议
https, 密文

  • http/0.9
    仅支持传输纯文本数据

  • http/1.0
    引入MIME机制,支持传输非文本数据(图片、视频、音频、动画)
    引入缓存机制,提升IO速度

  • http/1.1
    引入长连接(keepalive)机制,提升速度, 限制长连接的超时时间、最大请求数
    引入管道机制,提升速度, 支持同时发送多个请求
    增强缓存管理(静态数据、热点数据、过期时间)

  • http/2
    改进管道机制,支持请求、响应同时发送

二、nginx安装、启动

1、nginx介绍

跨平台、模块化
高并发 C10K、高性能
支持epoll(通知机制)事件驱动模型

2、nginx安装

  • 下载nginx安装包
[root@node01 ~]# wget https://nginx.org/download/nginx-1.26.2.tar.gz 
  • 安装依赖
[root@node01 ~]# yum install -y gcc openssl-devel zlib-devel pcre-devel 
  • 编译安装nginx
[root@node01 ~]# tar xf nginx-1.26.2.tar.gz 
[root@node01 ~]# cd nginx-1.26.2/
[root@node01 nginx-1.26.2]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
[root@node01 nginx-1.26.2]# make 
[root@node01 nginx-1.26.2]# make install 
  • nginx核心目录
安装目录/sbin:nginx命令
安装目录/conf:存放配置文件,主配置文件nginx.conf 
安装目录/logs: 存放日志,访问日志、错误日志
安装目录/html: 默认网页目录

3、nginx启动管理

  • 启动nginx
[root@node01 ~]# /usr/local/nginx/sbin/nginx 

[root@node01 ~]# netstat -tunlp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7975/nginx: master  

[root@node01 ~]# ps -elf | grep nginx 
1 S root       7975      1  0  80   0 - 11502 sigsus 14:24 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
5 S nobody     7976   7975  0  80   0 - 11614 ep_pol 14:24 ?        00:00:00 nginx: worker process

主进程:负责读取配置文件、记录日志、派生子进程 
工作进程:接收、处理客户端请求
  • 开机自启动
[root@node01 ~]# vim /etc/rc.d/rc.local 
/usr/local/nginx/sbin/nginx

[root@node01 ~]# chmod a+x /etc/rc.d/rc.local
  • 关闭nginx
[root@node01 ~]# /usr/local/nginx/sbin/nginx -s stop
  • 重新加载配置文件
[root@node01 ~]# /usr/local/nginx/sbin/nginx -s reload
  • 检测配置文件语法
[root@node01 ~]# /usr/local/nginx/sbin/nginx -t 
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  • 查看nginx版本、安装参数
[root@node01 ~]# /usr/local/nginx/sbin/nginx -V 
nginx version: nginx/1.26.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module

三、nginx配置文件

1、配置文件语法结构

全局配置

事件驱动模型
events {
}

http的配置 
http {
	server {
		location {
		}
		location {
		}
	}

	server {
	}
}

server {}:代表一个虚拟主机,虚拟主机支持在同一个服务器部署多套网站 
location{}:用于匹配客户端的访问请求,根据不同的请求做不同的处理

2、全局配置

  • 指定工作进程的用户
user nobody;
  • 指定工作进程的数量
worker_processes  8;
建议和CPU数量一致,或两倍
  • 定义错误日志、级别
error_log  logs/error.log  notice;
支持的级别: debug, info, notice, warn, error, crit, alert, or emerg
  • 定义pid文件
pid        logs/nginx.pid;

3、事件驱动模型的配置

events {
	  use epoll;
    worker_connections  4096;    // 每个工作进程处理的最大连接数
}
注意:nginx要运行BSD系列的Linux上,需要修改为use kqueue;

4、http的配置

  • 加载子配置文件
include       文件名称;
  • 定义访问日志、访问日志的格式
    统计网站的访问量、用户访问量、页面访问量
  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;

变量说明:
$remote_addr:客户端地址
$remote_user:客户端系统用户
$time_local:访问时间
$request:访问请求(请求方法、访问的文件名、http协议版本), 常见请求方法:GET、POST 
$status:状态码
$body_bytes_sent:响应数据的大小
$http_referer:超链接地址
$http_user_agent:客户端系统类型、浏览器
http协议的状态码:xxx 
200:成功响应
301、302、304:成功响应, 重定向
4xx: 错误		403: 权限拒绝、404文件找不到
5xx: 错误,服务端错误
  • 开启sendfile机制(零拷贝)
sendfile        on;
  • 长连接超时时间、最大请求数
    keepalive_timeout  65;
    keepalive_requests 1000;
  • 启用gzip压缩,节省带宽
gzip  on;
  • 网站服务的基本配置
server {
		listen		80;
		server_name	www.linux.com;

		location / {
				root	网页目录;
				index	index.html;
		}
}

四、虚拟主机配置

1、类型

基于名称的虚拟主机【常用】
基于IP地址的虚拟主机

2、基于名称的虚拟主机配置

blog.linux.com 网页目录: /web/blog
cart.linux.com 网页目录: /web/cart

  • 创建网页目录,测试首页
[root@node01 ~]# mkdir /web/blog -p

[root@node01 ~]# cat /web/blog/index.html
<h1> blog.linux.com </h1>
  • 编辑虚拟主机的配置文件
[root@node01 ~]# mkdir /usr/local/nginx/conf.d

[root@node01 ~]# cat /usr/local/nginx/conf.d/blog.conf
server {
   listen 80;
   server_name blog.linux.com;

   error_log  /usr/local/nginx/logs/blog_error.log error;
   access_log /usr/local/nginx/logs/blog_access.log main;

   location / {
       root /web/blog;
       index index.html;
   }
}
  • 在主配置文件中加载虚拟主机
[root@node01 ~]# vim /usr/local/nginx/conf/nginx.conf

http {

   include       /usr/local/nginx/conf.d/blog.conf;

}
  • 重新读取配置文件,测试访问
[root@node01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@node01 ~]# /usr/local/nginx/sbin/nginx -s reload

3、基于IP地址的虚拟主机配置

music.linux.com 192.168.140.20:80 网页目录: /web/music

  • 添加网卡,配置IP
[root@node01 ~]# ifconfig ens37
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.140.20  netmask 255.255.255.0  broadcast 192.168.140.255
        inet6 fe80::20c:29ff:fecc:6b39  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:cc:6b:39  txqueuelen 1000  (Ethernet)
        RX packets 1  bytes 60 (60.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 54  bytes 8030 (7.8 KiB)
  • 创建网页目录、测试网页
[root@node01 ~]# mkdir /web/music -p

[root@node01 ~]# cat /web/music/index.html
<h1> Music </h1>
  • 编辑配置文件
[root@node01 conf.d]# cat music.conf
server {
   listen 192.168.140.20:80;
   server_name music.linux.com;

   error_log  /usr/local/nginx/logs/music_error.log error;
   access_log /usr/local/nginx/logs/music_access.log main;

   location / {
       root /web/music;
       index index.html;
   }
}
  • 在主配置文件中加载
[root@node01 conf.d]# vim ../conf/nginx.conf

    include       /usr/local/nginx/conf.d/music.conf;
[root@node01 conf.d]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@node01 conf.d]# 
[root@node01 conf.d]# /usr/local/nginx/sbin/nginx -s reload

4、一些模块的使用

  • 访问控制模块
    默认允许所有客户端访问的

仅允许140.1访问

       allow 192.168.140.1;
       deny all;

禁止某个客户端访问(加入黑名单 )

	deny 192.168.140.1;
  • 自动列出网页目录下的文件
    默认为403
	autoindex on;
  • stub_status显示nginx的工作状态
   location /stat {
       stub_status;
       allow 192.168.140.1;
       deny all;
       access_log off;
   }
Active connections: 1 
server accepts handled requests
 3507 3507 3750 
Reading: 0 Writing: 1 Waiting: 0 

Active connections:当前的并发连接数
accepts:接收了多少连接
handled:处理了多少连接
reuqests:处理了多少请求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值