【nginx入门】

开源且高性能、可靠的http web服务、代理服务、负载均衡。

开源:直接获取源代码

高性能:支持海量并发

可靠:服务稳定

轻量:占用资源少

一、常见的web服务

httpd

IIS

GWS google开发

openrestry 基于nginx+lua

tengline 淘宝基于nginx开发

二、为什选择nginx

轻量,功能模块少,核心代码和http,其他由插件来安装

代码模块化,易读,便于二次开发

技术成熟

nginx 采用epoll网络模型,apache采用select模型

select:当用户发起一次请求,select模型会进行一次遍历扫描,导致性能低下。

epoll:当用户发起请求,epoll会直接进行处理,效率高,无连接限制。

三、nginx应用场景

静态服务:浏览器缓存,防资源盗用,资源分类,资源压缩,资源缓存,跨域访问

代理服务:协议类型,正向代理,反向代理,负载均衡,代理缓存,动静分离

安全服务:访问控制,访问限制,流量限制,拦截攻击,拦截异常请求,拦截sql注入

流行架构:Nginx+php_fastcgi_pass(LNMP),Nginx+java_proxy_pass(LNMT),nginx+Python(uwsgi_pass)

四、nginx安装

1.epel源

版本低,功能少 #不推荐

2.官方源

官方编译好的,封装成rpm包 #推荐

3.源码安装

自己下载源码,configure/make/make install

安装复杂,升级繁琐

官方选择稳定版本

vim etc/yum.repos.d/nginx.repo

[nginx-stable]

name=nginx stable repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=1

enabled=1

gpgkey=https://nginx.org/keys/nginx_signing.key

module_hotfixes=true

yum repoist #列出所有仓库

yum list nginx

必须是官方源的nginx

yum remove -y nginx

yum -y install nginx

nginx -v #查看版本

rpm -ql nginx #查询安装了那些文件

五、Nginx配置文件

/etc/nginx

/etc/nginx/nginx.conf

/etc/nginx/conf.d

/etc/nginx/conf.d/default.conf

cgi、fastcgi、uwsgi配置文件

/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

nginx日志轮询,切割

/etc/logrotate/nginx

nginx终端管理命令

/usr/bin/nginx

/user/sbin/nginx-debug

六、nginx模块目录

/etc/nginx/modultes

/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-.

/usr/share/man/man8/nginx.

nginx的缓存目录

/var/cache/nginx

nginx的日志目录

/var/log/nginx

nginx -V #查看源码安装选项

七、nginx配置文件详细

/etc/nginx/nginx.conf春文本类型文件,区块形式组织,使用大括号{}区分

1.coremodule 核心模块 全局配置

2.eventmodule事件驱动模块

3.httpcoreModule http内心模块 局部配置

coremodule: event http

http: server,配置多个网站X

server: location,访问路径

核心模块:

user nginx; #nginx进程用户

worker_processes 1; #nginx运行的work进程数量(建议与CPU数量一致)

error_log /log/nginx/error.log #错误日志路径

pid /var/run/nginx.pid #nginx服务后台pid进程号

events模块:

event {

worker_connections 1024; #每个worker进程最大连接数

use epoll; #事件驱动模型,epoll默认

}

http内核模块

http { #公共的配置定义在http

server { #server配置网站,每个server代表一个网站,简称虚拟主机

listen 80; #监听端口,默认80

server_name localhost; #提供服务的域名或主机名

access_log host.access.log main#访问日志

location / {

root /usr/share/nginx/html; #存放网站代码路径

index index.html index.htm; #服务器默认页面文件

}

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

}

include /etc/nginx/conf.d/*

}

/etc/nginx/mine.types #支持的文件的类型

cat /etc/nginx/mine.types

cd /etc/nginx/conf.d/

cat default.conf

egrep -v '^$|.*#' default.conf

八、nginx配置网站

1.在/etc/nginx/conf.d/下新建文档,备份mv default.conf default.conf.bak

vim game.conf

server {

listen 80;

server_name game.oldboy.com;

location / {

root /oldboy_code;

index index.html;

}

}

nginx -t #测试语法

mkdir -p /oldboy_code

cd o/oldboy_code 将代码放入

systemctl start nginx

systemctl reload nginx

nginx -s reload

排错:

1.解析问题

2.无法启动:

端口是否被占用

配置文件错误

3.配置文件写错,也有可能重载成功

4.其他错误,查看日志

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值