Docker 部署Nginx 访问日志时间不正确的解决方式
在运营网站的过程中,始终没有太把日志当回事。虽然开启了日志记录,不过没有仔细的分析过日志。今天心血来潮看日志时发现记录的时间要比服务器的时间少8个小时。 8个小时,这是一个比较特殊的数字。我们知道 UTC 时区是标准的世界通用时区,而UTC+8 是比世界通用时间快8个小时的时区,而我们正好是在这个时区的。 我的网站是使用Docker+Nginx部署的,因此这里推断Docker使用的时区可能是UTC。而我们的服务器所在的时区是比其快8小时的。 为了验证我们的猜想,使用如下命令进入docker 容器进行查看
$ docker exec -it nginx bash
进入容器之后,执行date
命令,显示如下结果:
很明显,docker容器的时区是UTC。知道了问题的所在,接下来就是校正docker容器的时区。网上目前基本上就三种方式:
- 共享主机的localtime
- 复制主机的localtime
- 创建自定义的dockerfile
我这里使用的是第一种方式,共享主机的localtime。我是使用的docker-compse
。所以直接在配置文件中新增一行文件映射即可
version: '3'
services:
web:
...
volumes:
...
- /etc/localtime:/etc/localtime
...
...
然后重新构建web镜像,并启动容器
$ docker-compose up web
这之后 nginx 再记录的日志的时间就和服务器同步了。
相关文章
How to Install Nginx on Ubuntu 20.04?
发布时间:2025/04/07 浏览次数:157 分类:OPERATING SYSTEM
-
Nginx is one of the most popular web servers in the world, responsible for hosting some of the largest and most trafficked sites on the Internet. It is a lightweight application software that can be used as a web server or a reverse proxy.
How to use Let's Encrypt with Nginx to configure https in Ubuntu 20.04
发布时间:2025/04/07 浏览次数:122 分类:OPERATING SYSTEM
-
Let's Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free TLS/SSL certificates, enabling encrypted HTTPS on your web server. It simplifies the process by providing a software client, Certbot, which a
Install WordPress with Nginx on Ubuntu 18.04
发布时间:2025/04/07 浏览次数:86 分类:OPERATING SYSTEM
-
WordPress is one of the most popular open source content management systems (CMS) with a market share of up to 60% compared to other CMS like Drupal or Joomla. WordPress can be used to develop any type of website, be it a blog, a small busi
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t