Nginx 学习笔记

一、简介

      Nginx (engine x) 是一个开源免费、轻量级高性能的 HTTP 和反向代理服务器,也是一个 IMAP / POP3 / SMTP 服务器。并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好。Nginx 具有三大功能:反向代理负载均衡动静分离。Nginx 支持常用的操作系统如:Linux 、Windows、macOS、HP-UX 等。截止到2021年11月22日,最新版本为:1.21.4。

      官网地址:https://nginx.org/

二、安装

A、Liunx 平台:

      此处以 CentOS 为例,可以使用 yum 命令进行安装:

yum install nginx -y

B、Windows 平台:

      从官网下载 Windows 平台的程序包,解压到任意目录,无需安装即可运行。(解压的路径中不能含有中文和其他特殊字符,否则会报错。)

三、常用命令

      此处以 Windows 平台为例,Linux 平台的命令类似。

1、查看 nginx 主程序版本号:nginx -v 

2、验证配置文件是否正确:    nginx -t

3、启动nginx :                          start nginx

4、正常停止 nginx :               nginx.exe -s quit

5、快速停止 nginx :               nginx.exe -s stop

6、加载修改后配置文件重启:nginx -s reload

四、核心配置

     Nginx 的核心配置文件为 conf/nginx.conf ,nginx 的核心功能在此配置文件中设置。

# nginx 核心配置文件
# 2021-11-15 yangqun2010@foxmail.com
# 工作进程:根据需要进行设置,一般设置为CPU核心数的2倍或直接设置为auto由程序自动调整
worker_processes auto;
# 错误日志路径
error_log  logs/error.log;
error_log  logs/error.log  notice;
events {
# 每个工作进程的连接数,默认为1024个
    worker_connections  1024;
	accept_mutex on;
}
http {
    include			mime.types;
    default_type	application/octet-stream;
# 解决 SSL/TLS 协议信息泄露漏洞(CVE-2016-2183)
	ssl_ciphers		HIGH:!aNULL:!MD5:!3DES;
# 隐藏 Nginx 版本号
	server_tokens	off;
# 日志格式
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
# 文件高效传输模式
    sendfile on;
# 上传文件最大大小,默认为1m,此处设置为1024m
  client_max_body_size 1024m;
# 与后端服务器的连接超时时间,默认为75秒。
	keepalive_timeout 75s;
# 开启压缩传输	
	gzip on;
# 压缩文件类型
	gzip_types application/javascript application/x-javascript text/javascript text/css application/json application/xml;
# 最小压缩文件
	gzip_min_length 4k;
# 压缩率
	gzip_comp_level 4;
# 客户端请求头buffer大小
	client_header_buffer_size 32k;
# 请求主体缓冲区
	client_body_buffer_size 8m;
# 服务器名字的哈希表大小
	server_names_hash_bucket_size 512;
# 头部哈希表的最大值
	proxy_headers_hash_max_size 51200;
# 头部哈希表大小
	proxy_headers_hash_bucket_size 6400;
    server {
# 监听端口,此处设置为http协议的默认端口80,访问时http时,无需加端口号
        listen       80;	
# 监听端口,此处设置为https协议的默认端口443,访问时https时,无需加端口号
		listen       443 ssl http2;
        server_name  localhost;
# https 协议证书,请放置在conf/下,证书可以购买或使用 mkcert 生成
		ssl_certificate      localhost.pem;
        ssl_certificate_key  localhost-key.pem;
# ssl协议版本
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
# 从系统时间中正则匹配出年月日,用于按天生成日志
		if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})") {
		set $date $1-$2-$3;
		}
# 访问日志的路径及文件名称,按天分割
        access_log  logs/localhost_access_$date.log  main;
        location / {
# 反向代理:后端服务地址,此处转发到本地的8080端口
        proxy_pass   http://127.0.0.1:8080;
# 反向代理:将客户端的真实的IP写入请求的 header 中供程序读取。
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header X-Forwarded-Host $http_host;
		proxy_set_header X-Forwarded-Port $server_port;
		proxy_set_header X-Forwarded-Proto $scheme;
# 错误代码跳转页面
		error_page	404              /404.html;
		error_page  500 502 503 504  /50x.html;
			location = /50x.html {
				root   html;
			}
       }
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值