When Nginx builds a file server, if there is a time zone problem, the modification time and access time of the file may be displayed incorrectly. To resolve this issue, you can follow these steps:
- Set the timezone in the Nginx configuration file. The time zone can be set
http
using directives in the blockenv
, for example:
这种方式不行,可能跟nginx版本有关
http {
# ...
env TZ=Asia/Shanghai;
# ...
}
This statement sets the time zone to Asia/Shanghai, which can be changed to another time zone if desired.
- Use directives in Nginx configuration files
autoindex_localtime on;
to enable local time display of files. For example:
server {
# ...
location /files/ {
autoindex on;
autoindex_localtime on;
# ...
}
# ...
}
This statement will display the local time of the file in the file listing instead of UTC time.
- Set the correct time zone in the file system. You can use
timedatectl
commands to set the system time zone, for example:
sudo timedatectl set-timezone Asia/Shanghai
This command sets the system time zone to Asia/Shanghai, which can be changed to other time zones as needed.
Note that if you are using a Docker container, you may need to set the time zone in the container. -e TZ=Asia/Shanghai
You can add the corresponding statement in the Dockerfile to set the time zone, or use the etc. option to set the time zone when starting the container . In addition, if you use a cache (such as FastCGI cache), you may need to adjust the cache time according to your needs to avoid cache expiration.