1.1 安装
1.1.1 安装前的准备
Nginx的安装需要确定Linux安装相关的几个库,否则配置和编译会出现错误,具体的检查安装过程为:
-
gcc编译器是否安装
检查是否安装:
yum list installed | grep gcc
执行安装:
yum install gcc -y
-
openssl库是否安装
检查是否安装:
yum list installed | grep openssl
执行安装:
yum install openssl openssl-devel -y
-
pcre库是否安装
检查是否安装:
yum list installed | grep pcre
执行安装:
yum install pcre pcre-devel -y
-
zlib库是否安装
检查是否安装:
yum list installed | grep zlib
执行安装:
yum install zlib zlib-devel -y
-
一次性安装,执行如下命令
yum install gcc openssl openssl-devel pcre pcre-devdl zlib zlib-devel -y
1.1.2 正式安装
解压下载下来的nginx文件,执行命令:tar -zxvf nginx-1.14.2.tar.gz
切换至解压后的nginx主目录,执行命令:cd nginx-1.14.2
在nginx主目录nginx-1.14.2下执行命令:./configure --prefix=/usr/loacl/nginx
(其中--prefix是指定nginx安装路径)注意:等号左右不要有空格
执行命令进行编译:make
执行命令进行安装:make install
安装成后,可以切换到/usr/local/nginx目录下,查看内容
查看:ll
nginx 包括文件内容:conf(nginx的配置文件)、 html(网页地址)、logs(日志记录)、sbin(nginx启动文件);
1.2 启动
查看Nginx安装安装路径
where is nginx
1.2.1 普通启动
切换到nginx安装目录的sbin目录下,执行: ./nginx
第一步:cd sbin/
第二步:pwd
第三步:./nginx
查看启动进程:ps -ef | grep nginx
进程包含:master process 和 worker process
1.2.2 通过配置文件启动
./nginx -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
其中-c是指定配置文件,而且配置文件路径必须指定绝对路径
1.2.3 检查Nginx是否启动
通过查看进程:ps -ef | grep nginx
nginx 体系结构由 master 进程和其 worker 组成
master 进程读取配置文件,并维护 worker 进程,而 worker 进程则对请求进行实际处理
Nginx启动后,安装目录下会出现一些 tmp 结尾的文件,这些事临时文件,不用管。
1.3 关闭
1.3.1 优雅关闭Nginx
找出nginx的进程号:ps -ef | grep nginx
执行命令:kill -QUIT 主Pid
注意:1.其中pid是主进程号的pid(master process),其他为子进程pid(worker process)
2.这种关闭方式会处理完请求后在关闭,所以称之为优雅的关闭
1.3.2 快速关闭Nginx
找出nginx的进程号:ps -ef | grep nginx
执行命令:kill -TERM 主pid
> 注意:1.其中pid是主进程号的pid(master process),其他为子进程(worker process)
>
> 2.这种关闭方式不管请求是否处理完成,直接关闭,比较暴力,称之为快速的关闭
1.3.3 重启Nginx
./nginx -s reload
1.4 配置检查
当修改Nginx配置文件后,可以使用Nginx命令进行配置文件语法检查,用于检查Nginx配置文件是否正确。
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf -t
1.5 其它
Linux 上查看nginx版本:/usr/local/nginx/sbin/nginx -V
-v (小写的v)显示nginx的版本
-V(大写的V)显示nginx的版本,编译器版本和配置参数
2.1 Nginx的核心配置文件
学习Nginx首先需要对它的核心配置文件有一定的认识,这个文件位于Nginx的安装目录/usr/local/nginx/conf目录下,名字为nginx.conf.
详细配置,可以参考resources目录下的《nginx配置中文详解.conf》
2.1.1 基本配置
配置worker进程运行用户 nobody 也是一个linux用户,一般用于启动程序,没有密码
#user nobody;
配置工作进程数目,根据硬件调整,通常等于CPU数量或者2倍于CPU数量 worker_processes 1;
配置全局错误日志及类型,[ debug|info|notice|warn|error |crit ],默认是error error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid #配置进程pid文件
2.1.2 events 配置
>配置工作模式和链接书
>
>```
>events {
> worker_connections 1024; #配置每一个worker进程连接数上限,nginx支持的总连接数就等于 worker_processes * worker_connections,取值上限:65535。
>}
>```
2.Nginx主要应用
- 静态网站部署
- 负载均衡
- 静态代理
- 动静分离
- 虚拟主机
3.静态网站部署
Nginx是一个HTTP的web服务器,可以将服务器上的静态文件(如HTML、图片等)通过HTTP协议返回给浏览器客户端.
location / {
root html;
index index.html index.htm;
}
location /ace {
root /opt/www;
index index.html index.htm;
}
root值指项目根路径 等于(ip+端口),“/” = “/opt/www”;“/ace” =“/opt/www/ace”
3.1 Nginx.conf配置文件样例
列-1(包含本地Api接口转发)
Nginx.conf 路径:/usr/Nginx/conf/nginx.conf
#user nobody;
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 {
worker_connections 1024;
}
http {
include 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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream api{
server 127.0.0.1:9091;
}
server {
listen 9092;
server_name x.xxxxx.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /home/web/pcms_PC;
#index index.html index.htm;
}
location /bim {
alias /home/web/pcms_BIM;
#index index.html index.htm;
}
location /api {
proxy_pass http://api/;
}
#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;
# }
#}
}
3.2 Nginx 配置识别移动端(Android/iOS/Ipad等)拦截转发
# nginx.conf 例:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
#dev环境
server {
#监听的端口
listen 8001;
server_name localhost;
#设置日志
# access_log logs/dev.access.log main;
#定位到index.html
location / {
root /usr/share/nginx/html/dev/dist;
if ($http_user_agent ~* "(android|iphone|ipod|ipad)") {
root /usr/share/nginx/html/uat/dist;
}
index index.html;
}
}
#sit环境
server {
#监听的端口
listen 8002;
server_name localhost;
#设置日志
# access_log logs/sit.access.log main;
#定位到index.html
location / {
#linux下HTML文件夹,就是你的前端项目文件夹
root /usr/share/nginx/html/sit/dist;
# root /home/html/dev/dist;
#输入网址(server_name:port)后,默认的访问页面
index index.html;
try_files $uri $uri/ /index.html;
}
}
# include /etc/nginx/conf.d/*.conf;
}
4.实操步骤(按照列-1)
步骤一:配置修改nginx.conf
cd /usr/Nginx/conf
vim nginx.conf
wq 保存并退出,重新启动Nginx
第一步:cd sbin/
第二步(检查nginx.conf编写是否正确):./nginx -t
第三步:./nginx -s reload
步骤二:查看Nginx中所用端口是否开启防火墙
第一步查看端口情况(查询端口号80 是否开启):
firewall-cmd --query-port=80/tcp
第二步开启80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
第三步重启防火墙:
firewall-cmd --reload
(查询有哪些端口是开启的: firewall-cmd --list-port)
步骤三:打开浏览器,输入对应的IP+端口即可访问。
5.负载均衡
5.1 负载均衡概述
在网上创立初期,我们一般都使用单台机器对外提供集中式服务。随着业务量的增大,我们一台服务器不够用,此时就会把多台机器组成一个集群对外提供服务,但是,我们网站对外提供的访问入口通常只有一个,比如:www.web.com。 那么当用户在浏览器输入www.web.com 进行访问的时候,如何将用户的请求分发到集群中不同的机器上呢,这就是负载均衡要做的事情。
负载均衡通常是指将请求“均匀”分摊到集群中多个服务器节点上执行,这里的均匀是指在一个比较大的统计范围内是基本均匀的,并不是完全均匀。