目录
简介
一、docker安装
docker安装命令
修改主页内容
docker run参数详情
nginx.conf配置详情
default.conf配置详情
1.配置一台服务器实现反向代理
2.配置两台服务器实现负载均衡
二、宿主机安装
1、安装nginx依赖的库
编辑
三、启动操作
1.nginx启动
2.配置systemctl启动
问题:
1.执行完systemctl start nginx.service 后报错
2.nginx: [emerg] unknown directive "ssl_certificate" in /usr/local/nginx/conf/
配置ssl参考
简介
Nginx是一个web服务器也可以用来做负载均衡、反向代理及动静分离使用,目前使用最多的就是负载均衡
一、docker安装
docker安装命令
# 下载镜像
docker pull nginx:latest
# 启动nginx
docker run --name nginx -p 81:80 -d nginx
# 创建nginx挂载路径
mkdir -p data/nginx/{conf,html,log}
# 复制nginx配置文件到宿主机
docker cp nginx:/etc/nginx/conf.d /data/nginx/conf/
docker cp nginx:/etc/nginx/nginx.conf /data/nginx/
docker cp nginx:/usr/share/nginx/html/ /data/nginx/
docker cp nginx:/var/log/nginx/ /data/nginx/log/
# conf.d : 存放具体项目的nginx配置文件
# .conf : 存放外置nginx配置文件
# html : 存放Vue包
# log : 存放nginx日志文件
# 可将打包好的Vue项目包(一个名为dist的文件夹)放到 /data/nginx/html 目录下。
# 这样创建挂载容器的时候要改成 -v /data/nginx/html/dist/:/usr/share/nginx/html
# 删除现有的容器
docker rm -f nginx
# 创建nginx网络 -- 可以不建,如果不建,下面请不要执行--network nginx_network
docker network create nginx_network
# 使用docker命令创建nginx并挂载(注意:修改nginx.conf文件后,必须重启nginx容器服务!!!)
docker run --name nginx -p 81:80 \
-v /data/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /data/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /data/nginx/html/:/usr/share/nginx/html/ \
-v /data/nginx/log/:/var/log/nginx/ \
--privileged=true \
-d \
--network nginx_network \
nginx:latest修改主页内容
cd html/
vi index.html修改完 wq 出去
访问ip:81

查看日志
docker logs -f --tail 100 nginx
docker run参数详情
// 后台运行
-d
// 交互式运行
-i
// tty终端
-t
// 容器名称
--name
//设置容器里面的主机名,登陆到容器里面可以看到
--hostname
// 授予此容器扩展特权,也就是开启特权模式;这种模式下容器对docker宿主机拥有root访问权限,允许我们以其宿主机具有(几乎)所有能力来运行容器,包括一些内核特性和设备访问,慎用
--privileged=true
// no:为默认值,表示容器退出时,docker不自动重启容器
// on-failure:若容器的退出状态非0,则docker自动重启容器,还可以指定重启次数,若超过指定次数未能启动容器则放弃
// always : 只要容器退出,则docker将自动重启容器
--restart always
// 如果容器启动时没有设置--restart参数,则通过下面命令进行更新,2ef06a364009是容器的ID
docker update --restart=always 2ef06a364009
docker update --restart=on-failure:3 2ef06a364009
// 数据卷映射 宿主机目录:容器目录。这样方便我们管理容器配置文件及日志文件
-v
//端口映射,前面为宿主机的端口(一般设置这个端口),后面为容器服务进程端口,访问宿主机的80,最终会转发给容器的80端口
-p 81:80
// 设置容器CPU和内存的使用上限
--cpus=1 -m 512Mnginx.conf配置详情
# ===================全局块开始======================
user nginx;
# 工作进程数,一般配置成和cpu数量一致
worker_processes auto;
#全局错误日志及pid文件存放位置
error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#nginx 启动master进程pid号
pid /var/run/nginx.pid;
# =================全局块结束============================
#==============events块开始======================
events {
#标识单个worker进程的最大并发数
worker_connections 1024;
}
#============events块结束============================
#============http块开始(nginx服务器中配置最频繁的部分,配置虚拟主机,监听端口,请求转发等等)==========================
http {
#引入 mime 类型定义文件
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压缩
#gzip on;
#配置服务文件,* 代表引用全部
include /etc/nginx/conf.d/*.conf;
}default.conf配置详情
1.配置一台服务器实现反向代理
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
#被代理服务的地址
proxy_pass http://192.168.149.166:9090;
}
#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 /usr/share/nginx/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;
#}
}访问ip:81

2.配置两台服务器实现负载均衡
轮询(默认)
#此处配置多台tomcat服务器
upstream webServer{
server 192.168.149.166:9090;
server 192.168.149.167:9090;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
#此处需要改下
proxy_pass http://webServer;
}
#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 /usr/share/nginx/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;
#}
}weight权重:
weight 代表权重默认为 1,权重越高被分配的客户端越多
upstream myserver {
server 192.168.80.102:8081 weight=1;
server 192.168.80.102:8082 weight=2;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}ip_hash:
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决session问题
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
ip_hash;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}fair
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream myserver {
server 192.168.80.102:8081;
server 192.168.80.102:8082;
fair;
}
server {
listen 80;
location / {
proxy_pass http://myserver;
}
}二、宿主机安装
1、安装nginx依赖的库
主要包括:GCC,PCRE,zlib,OpenSSL,执行下面的命令一次性安装即可
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel2.上传安装包并解压
如果没有提前下载安装包,也可以使用wget进行下载
wget http://nginx.org/download/nginx-1.19.1.tar.gz3.解压安装包
tar -xvf nginx-1.19.1.tar.gz4.进入nginx资源文件,配置 confifigure
执行 命令 : ./configure ,这个脚本是用来生成nginx相关文件的目录的,在复杂的安装模式中是可以指定其他参数的,比如path ,这里使用默认的 ,默认情况下,安装完毕后,主目录就在 /usr/local/nginx中

5.执行make 和 make install
make && make install6.启动nginx服务
来到 /usr/local/nginx目录下,进入 sbin目录进行启动即可

三、启动操作
1.nginx启动
cd /usr/local/nginx/sbin/
1.1 启动 ./nginx
1.2 停止 ./nginx -s stop
1.3 重启 ./nginx -s reload
2.配置systemctl启动
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx配置方式
2.1 创建一个nginx.service
在 /usr/lib/systemd/system/目录下面新建一个nginx.service文件。并赋予可执行的权限
vim /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service
2.2 编辑service内容
[Unit] //对服务的说明
Description=nginx - high performance web server //描述服务
After=network.target remote-fs.target nss-lookup.target //描述服务类别
[Service] //服务的一些具体运行参数的设置
Type=forking //后台运行的形式
PIDFile=/usr/local/nginx/logs/nginx.pid //PID文件的路径
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf //启动准备
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf //启动命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload //重启命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop //停止命令
ExecQuit=/usr/local/nginx/sbin/nginx -s quit //快速停止
PrivateTmp=true //给服务分配临时空间
[Install]
WantedBy=multi-user.target //服务用户的模式2.3 启动服务
在启动服务之前,需要先重载systemctl命令
systemctl daemon-reload
systemctl start nginx.service问题:
1.执行完systemctl start nginx.service 后报错
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
解决步骤:
1).检查端口是否被占用
netstat -tnlp如果端口已经被占用,自己权衡一下是换个端口还是把占用端口的进程杀掉
2).检查nginx是否已经启动
ps -aux | grep nginx
如果已经启动使用下面命令干掉即可
pkill -9 nginx2.nginx: [emerg] unknown directive "ssl_certificate" in /usr/local/nginx/conf/
配置这个SSL证书需要引用到nginx的中SSL这模块,一开始编译的Nginx的时候并没有把SSL模块一起编译进去,所以导致这个错误的出现。
解决步骤:
1).在nginx的安装目录(/usr/local/nginx-1.19.1)执行
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module2).在nginx安装目录执行
make千万不要make install 否则会覆盖现有的nginx
3)重启nginx
















