一、简介
Nginx 是高性能的 HTTP 和反向代理的web服务器,占用内存小,并发能力强。支持高达 50,000 个并发连接数
二、作用
反向代理、负载均衡、动静分离
正向代理:正向代理服务器位于客户端和服务器之间,是代理服务器来访问服务器的过程。需要在客户端配置代理服务器进行指定网站访问(为了从服务器获取数据,客户端要向代理服务器发送一个请求,并指定目标服务器,代理服务器将目标服务器返回的数据转交给客户端。)
反向代理:客户端对代理是无感知的,客户端不需要任何配置就可以访问,客户端将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。
三、安装
1、windows下安装
打开官网 http://nginx.org/en/download.html 下载windows版本,直接解压即可
2、linux下安装
- 上传安装包
将官网下载的nginx安装包上传至虚拟机 - 解压
tar -zxvf nginx-1.22.1.tar.gz
- 进入解压文件
cd nginx-1.22.1/
4.执行配置
./configure
- 编译一下
make && make install
- 验证,成功会看到安装目录
[root@localhost nginx-1.22.1]# whereis nginx
nginx: /usr/local/nginx
3、linux下安装可能会遇到的问题
问题1:./configure 执行配置文件时,报以下错误:
checking for OS
Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
解决1:需要c语言支持,下载gcc即可
yum -y install gcc
问题2:在下载gcc之后,使用make命令编译时报以下错误
make: *** 没有规则可以创建“default”需要的目标“build”。 停止。
解决:
安装nginx需要的依赖
yum install pcre-devel zlib zlib-devel openssl openssl-devel
重新执行配置
./configure
最后再编译
make && make install
四、nginx启动
#查看nginx目录:whereis nginx
[root@localhost nginx-1.22.1]# whereis nginx
nginx: /usr/local/nginx
#进入目录
cd /usr/local/nginx/
#进入可执行文件
cd ./sbin/
#运行nginx
./nginx
#验证是否成功
#浏览器打开80端口,如下图所示即启动成功
五、nginx常用命令
cd /usr/local/nginx/sbin/ #进入可执行文件
./nginx #启动nginx
./nginx -s stop #停止
./nginx -s quit #安全退出
./nginx -s reload #重新加载配置文件
ps aux | grep nginx #查看进程
六、nginx配置文件初步解读
配置文件内容虽多,其实主要由3部分组成:全局块、events、http块
全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
evevts块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
########################全局块########################
#配置用户、用户组 默认为nobody
#user nobody;
#允许的进程连接数
worker_processes 1;
#日志路径 日志级别为:debug info notice war
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#nginx进程运行文件存放地址
#pid logs/nginx.pid;
########################evevts块########################
#events网络连接相关配置
events {
#每个worker_processes 最大连接数
worker_connections 1024;
}
########################http块########################
http {
#########http全局配置########
include mime.types; #文件扩展名与文件类型映射表
default_type application/octet-stream; #默认返回类型,可以在server.location里面改
#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;
sendfile on; #允许sendfile方式传输文件,可以在http块,server块,location块。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间(秒)
#gzip on;
server {
listen 80; #监听端口
server_name localhost; #监听地址
#charset koi8-r;
#access_log logs/host.access.log main;
#访问本机80端口根目录 就会走
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#指定错误页面,状态码是5开头的会跳转到该页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}