Nginx的安装
# 安装编译工具及库文件
[root@localhost usr]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
# 下载PCRE,PCRE的作用是让Nginx支持rewrite功能
[root@localhost usr]# wget https://sourceforge.net/projects/pcre/files/pcre/8.44/pcre-8.44.tar.gz
# 解压缩PCRE
[root@localhost usr]# tar -zxvf pcre-8.44.tar.gz
# 进入安装包目录
[root@localhost usr]# cd pcre-8.44
# 编译安装
[root@localhost pcre-8.44]# ./configure
[root@localhost pcre-8.44]# make && make install
# 查看PCRE的版本
[root@localhost pcre-8.44]# pcre-config --version
# 下载Nginx
[root@localhost usr]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压缩Nginx
[root@localhost usr]# tar -zxvf nginx-1.18.0.tar.gz
# 进入安装目录
[root@localhost usr]# cd nginx-1.18.0
# 编译安装
[root@localhost nginx-1.18.0]# ./configure
[root@localhost nginx-1.18.0]# make && make install
# 把Nginx加到环境变量中,就是在/usr/local/bin下加了一个软链接
[root@localhost nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin
# 查看Nginx版本号
[root@localhost nginx-1.18.0]# nginx -v
Nginx的常用命令
# 查看Nginx版本号
[root@localhost ~]# nginx -v
nginx version: nginx/1.18.0
# 启动Nginx
[root@localhost nginx-1.18.0]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ./nginx
[root@localhost sbin]# ps -ef |grep nginx
root 65162 1 0 20:00 ? 00:00:00 nginx: master process ./nginx
nobody 65163 65162 0 20:00 ? 00:00:00 nginx: worker process
root 65214 63670 0 20:01 pts/1 00:00:00 grep --color=auto nginx
# 重新加载Nginx
[root@localhost sbin]# nginx -s reload
# 关闭Nginx
[root@localhost sbin]# nginx -s stop
# 安全退出Nginx(处理完请求再退出)
[root@localhost sbin]# nginx -s quit
Nginx的配置文件
Nginx配置文件的位置:/usr/local/nginx/conf/nginx.conf。
将Nginx的配置文件分为三部分:全局块、events块、http块。
这里简单了解下,后面在具体应用场景中了解配置文件各个部分的使用。
全局块
从配置文件开始到events块之间的内容,主要会设置一些影响Nginx服务器整体运行的配置指令,主要包括配置运行Nginx服务器的用户(组)、允许生成的worker process数,进程PID存放路径、日志存放路径和类型以及配置文件的引入等。
# Nginx的用户和组
#user nobody;
# 工作进程数,根据硬件进行调整,通常等于CPU数量或者CPU数量的两倍
worker_processes 1;
# 错误日志存放路径
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 进程标识符
#pid logs/nginx.pid;
events块
events块涉及的指令主要影响Nginx服务器与用户的网络连接,常用的设置包括是否开启对多worker process下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个worker process可以同时支持的最大连接数等。
这部分对Nginx的性能影响较大,实际中需要灵活配置。
events {
# 每个工作进程最大连接数是1024个,根据硬件来调整
# 理论上一台Nginx的最大连接数 = worker_processes × worker_connections
worker_connections 1024;
}
http块
http块是Nginx中使用最频繁的部分。http块包括http全局块和server块。
http全局块
http全局块配置的指令包括文件引入、MIME-TYPE定义、日志自定义、连接超时时间、单链接请求数上限等。
server块
server块和虚拟主机有密切关系,每个http块可以包含多个server块,每个server块相当于一个虚拟主机。每个server块又包含server全局块、多个location块。
server全局块
用于虚拟机监听配置,虚拟主机名称和IP配置。
location块
基于Nginx服务器接收到的请求路径,对虚拟主机以外的路径进行匹配,对特定请求进行特定处理。
http {
# 设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
# 日志格式设置
# $remote_addr与$http_x_forwarded_for用以记录客户端的ip地址
# $remote_user:用来记录客户端用户名称
# $time_local:用来记录访问时间与时区
# $request:用来记录请求的url与http协议
# $status:用来记录请求状态;成功是200
# $body_bytes_sent:记录发送给客户端文件主体内容大小
# $http_referer:用来记录从那个页面链接访问过来的
# $http_user_agent:记录客户浏览器的相关信息
# 通常Web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的IP地址
# 反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 指定log文件的路径
#access_log logs/access.log main;
# 指定Nginx是否调用sendfile函数(zero copy方式)来输出文件,对于普通应用,必须设为on
sendfile on;
# 允许或禁止使用socke的TCP_CORK的选项,仅在使用sendfile的时候使用
#tcp_nopush on;
# keepalive超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 监听80端口
listen 80;
# 主机名称是localhost
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 用于匹配请求中的路径,这个location匹配的是请求中的/,这里的匹配支持正则表达式匹配,如果一个请求同时满足了多个location,会采用最长前缀匹配的规则
location / {
root html;
index index.html index.htm;
}
# 错误页面
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
# 错误页面
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;
# }
#}
}