How to solve the time zone problem when nginx builds a file server: time display error

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:

  1. Set the timezone in the Nginx configuration file. The time zone can be set httpusing directives in the block env, 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.

  1. 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.

  1. Set the correct time zone in the file system. You can use timedatectlcommands 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/ShanghaiYou 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.

Guess you like

Origin blog.csdn.net/a772304419/article/details/131975873