这段时间在研究docker,在部署nginx时遇到了坑,最主要的问题是在挂载文件和文件夹的时候不知道怎么挂载,经过反复实验以及查看网上的教程,先总结如下:
记住项目端口号,下面挂在配置需要使用
yum install docker
或
sudo wget -qO- https://get.docker.com | sh
docker version
或
docker -v
service docker start
或
#centos7开始的版本可以执行systemctl命令,注意自己服务器的版本
systemctl start docker
systemctl enable docker
设置自启成功:
至此docker安装完毕
docker run --name=nginx -p 80:80 -d nginx
docker cp nginx:/etc/nginx/conf.d/default.conf /usr/local/nginx/conf.d/
文件内容(修改):
server {
listen 80;#配置监听端口
listen [::]:80;
server_name xxx.xxx.xxx;#配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://公网ip:项目端口号;#切记不要使用localhost当ip
#当没一个server里面没有其他的路径可以访问时会默认访问该配置目录下的文件
root /usr/share/nginx/html;
#首页定义默认访问的是哪个文件 如果index.html没有找到就会去找index.htm
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
#修改404 状态码的对应的指向的访问目录,修改后需重启服务器。
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;
#}
}
docker cp nginx:/etc/nginx/nginx.conf /usr/local/nginx/
文件内容:
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;
include /etc/nginx/conf.d/*.conf;
}
docker kill nginx
docker rm nginx
docker pull nginx
docker run --name=nginx -p 80:80
-v /usr/local/nginx/conf.d:/etc/nginx/conf.d
-v /usr/local/nginx/nginx.conf:/etc/nginx/nginx.conf
-d nginx
启动成功:
若在启动过程中遇到如下问题:
说明Linux和docker版本兼容问题,多半是docker版本过低,解决方法如下:
a. 更新yum包
yum update
b. 卸载已安装的docker(如果安装过的话)
yum remove docker docker-common docker-selinux docker-engine
c. 安装依赖
yum install -y yum-utils device-mapper-persistent-data lvm2
d. 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
(官方下载过慢) 替换文件中的地址为清华大学的地址:
sed -i 's+download.docker.com+mirrors.tuna.tsinghua.edu.cn/docker-ce+' /etc/yum.repos.d/docker-ce.repo
e. 安装Docker
sudo yum makecache fast
sudo yum install docker-ce
f. 配置Docker的镜像加速器
这里采用阿里云的镜像加速服务,具体位置见图。
vim /etc/docker/daemon.json
写入以下内容,注意替换你的加速地址\
{
"registry-mirrors": ["https://你的加速器地址.mirror.aliyuncs.com"]
}
systemctl start docker
systemctl enable docker
如此问题应该可以解决,重复挂载配置准备开始的流程.
请求项目里的GET请求测试,出现如下页面代表nginx部署成功(域名不带端口号):
如果出现域名DNS解析失败或者服务器找不到ip等问题,确保域名已备案/DNS解析配置正确,对应的服务器到对应的DNS配置里查看,一般都应该有对应的域名诊断工具(如:腾讯云域名诊断工具).
到此可以使用域名+URL的方式调用接口
以腾讯云为例:
server {
listen 80;#配置监听端口
listen [::]:80;
listen 443 ssl;
#填写绑定证书的域名
server_name 域名;
#证书文件名称
ssl_certificate /ssl/XXX.crt;
#私钥文件名称
ssl_certificate_key /ssl/XXX.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_pass http://公网ip:端口号;
root /usr/share/nginx/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 /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;
#}
}
docker run --name=nginx -p 443:443 -p 80:80 -v /usr/local/nginx/conf.d:/etc/nginx/conf.d -v /usr/local/nginx/nginx.conf:/etc/nginx/nginx.conf -v /usr/local/nginx/ssl/:/ssl/ -d nginx