Nginx学习——Day01 Nginx入门

时间:2019-11-27
本文章向大家介绍Nginx学习——Day01 Nginx入门,主要包括Nginx学习——Day01 Nginx入门使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.Nginx是一个开源且高性能、可靠的HttpWeb服务、代理服务。

开源: 直接获取源代码
高性能: 支持海量并发
可靠: 服务稳定

nginx+lua 可实现waf防火墙 (7层应用层防火墙 http://)
传统防火墙工作在四层tcp/udp (22|80)
1.花钱解决问题()
2.自己解决问题(浪费时间、没有上浮空间、)

2.为什么选择 Nginx

1.Nginx非常轻量   

  1.功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)
  2.代码模块化(易读,便于二次开发,对于开发人员是非常友好)

2.互联网公司都选择Nginx   

  1.技术成熟, 大公司都选择Nginx
  2.统一技术选型工具Nginx, 降低维护成本,减少故障几率。
  3.Nginx涉足场景较多,技术更新成本低。

3.Nginx采用Epool网络模型, Apache采用Select模型。

  Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。
  Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

3.Nginx 应用场景

静态处理(mp4|html|png|jpg) -> 服务端存放的是什么,客户端浏览器就展示什么
代理服务
正向代理
反向代理
代理缓存()
负载均衡
安全服务
访问限制(tcp连接数、http请求数)
访问认证(用户和密码、来源IP)
安全防护(waf防火墙,使用lua实现的,花钱买服务,花钱买经验)
架构

Nginx安装

  1.epel仓库=>Nginx(1.版本低 2.配置文件不一样)
  2.源码编译=>Nginx(1.复杂 2.企业不使用)
  3.官方仓库=>Nginx)(√1.版本较新 2.安装简单 3.配置不复杂)

1.配置Nginx官方的仓库

[root@web01 ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

2.安装Nginx【一定确认是通过官方的仓库安装上】

[root@web01 ~]# yum install nginx -y

3.检查版本【1.16.1】

[root@web01 ~]# nginx -v
nginx version: nginx/1.16.1

4.查看nginx编译的参数

[root@web01 ~]# nginx -V
nginx version: nginx/1.16.1
...
...
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pind-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

5.编译参数越多越好,还是越少越好?

  源码编译好了,做成的rpm包

  越少:功能少,后期可维护性差
  越多:功能全,覆盖广,可维护性强

Nginx配置文件

Nginx主配置文件
/etc/nginx
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
# /etc/nginx/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始与结束。

Cgi、Fastcgi、Uwcgi配置文件
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params

Nginx编码转换映射文件
/etc/nginx/win-utf
/etc/nginx/koi-utf
/etc/nginx/koi-win

http协议的Content-Type与扩展名
/etc/nginx/mime.types

配置系统守护进程管理器
/usr/lib/systemd/system/nginx.service
systemctl reload nginx
/usr/lib/systemd/system/nginx-debug.service

Nginx日志轮询,日志切割
/etc/logrotate.d/nginx

Nginx终端管理命令
/usr/sbin/nginx
/usr/sbin/nginx-debug

Nginx模块目录
/etc/nginx/modules
/usr/lib64/nginx
/usr/lib64/nginx/modules

Nginx默认站点目录
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html

Nginx帮助手册
/usr/share/doc/nginx-1.16.1
/usr/share/doc/nginx-1.16.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz

Nginx缓存目录
/var/cache/nginx

Nginx日志目录
/var/log/nginx 

1.CoreModule 核心模块 全局配置

2.EventModule 事件驱动模块 全局配置

3.HttpCoreModule http内核模块 局部配置

需了解扩展项
CoreModule层下可以有Event、HTTP
HTTP模块层允许有多个Server层, Server主要用于配置多个网站
Server层又允许有多个Location, Location主要用于定义网站访问路径

http {
server {
listen 80
server_name www.test.com
location / {
root /test_code/www
index index.html
}
}
}
[root@web01 ~]# cat /etc/nginx/nginx.conf 

# CoreModule 核心模块
user nginx; # 运行nginx程序的用户
worker_processes 1; # 运行的进程数量
error_log /var/log/nginx/error.log warn; # 错误日志
pid /var/run/nginx.pid; # 存放nginx进程运行的pid

# events事件模块
events { # 事件模块开始
worker_connections 1024; # worker进程的最大连接数
use epool; # 事件使用的模型(默认epool)
} # 事件模块结束

# http内核模块
http { # http开始
include /etc/nginx/mime.types; # 包含
default_type application/octet-stream; #

# 定义日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

# 访问日志存放的路径【main是日志的格式】
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65; # 长连接
#gzip on; # 压缩
include /etc/nginx/conf.d/*.conf; # 所有的conf结尾的文件都被包含起来

server { # 我要定义一个网站【博客】
listen 80; # 监听80端口
server_name localhost; # 对应的域名

location / { # 用户请求域名时,默认匹配的规则
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm; # 返回的默认页面
}

error_page 500 502 503 504 /50x.html; # 定义错误页面的
}
}

部署一个站点:

1.对应的nginx配置文件

[root@web01 conf.d]# cat /etc/nginx/conf.d/test_game.conf 
server {
listen 80;
server_name game.test.com;

location / {
root /test_code;
index index.html;
}
}

2.对应的源代码文件【手动-太low】

[root@web01 conf.d]# mkdir /test_code
[root@web01 conf.d]# cd /test_code/
[root@web01 test_code]# rz html5.zip
[root@web01 test_code]# unzip html5.zip
[root@web01 test_code]# pwd
/test_code
[root@web01 test_code]# ls
ceshi game html5.zip img index.html readme.txt

3.检查nginx的语法(每次变动都进行,形成习惯)

[root@web01 test_code]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

4.重载Nginx【reload|restart】

[root@web01 test_code]# #nginx -s reload
[root@web01 test_code]# systemctl reload nginx

5.如何访问:

1.通过服务器的IP直接访问:10.0.0.7
2.通过域名方式访问
Windows: C:\Windows\System32\drivers\etc\hosts 文件
Mac: sudo vim /etc/hosts
10.0.0.7 game.test.com

 排错:

  1、解析问题
  2、nginx无法启动
    (1) 是否端口被占用
    (2) 配置文件写错
  3、重载问题
    配置文件写错,一定要nginx -t语法测试
  4、一定看日志

原文地址:https://www.cnblogs.com/ahlyu/p/11944665.html