[Linux服务器 ] nginx 配置图片静态访问方法

一.  Nginx安装

1.更新软件包列表:打开终端,并使用以下命令更新软件包列表:

sudo yum update
2.安装 EPEL 存储库:EPEL 存储库提供了额外的软件包,其中包括 Nginx。使用以下命令安装 EPEL 存储库:

sudo yum install epel-release
3.安装 Nginx:使用以下命令安装 Nginx:

sudo yum install nginx

4.启动 Nginx:安装完成后,可以使用以下命令启动 Nginx 服务:

sudo systemctl start nginx

5.配置开机自启:使用以下命令将 Nginx 设置为开机自启动:

sudo systemctl enable nginx
6.验证安装:打开 web 浏览器,并输入服务器的 IP 地址或域名。如果一切正常,你将看到 Nginx 的默认欢迎页面。

Nginx 的配置文件通常位于 /etc/nginx/nginx.conf/etc/nginx/conf.d/default.conf。根据你的需求,可以进行相应的修改。

[Linux服务器 ] nginx 配置图片静态访问方法_第1张图片

 

二. 进入/etc/nginx/nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}


http {

    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;


        location /var/ {
                alias /var/;
                autoindex on;
            }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
    



# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

修改location配置  为自己访问图片的位置   第五行 user改为root

        location /var/ {
                alias /var/;  # 访问图片位置
                autoindex on;
            }

重新启动 Nginx 服务,使配置更改生效。可以使用以下命令来重启 Nginx:

sudo service nginx restart

[Linux服务器 ] nginx 配置图片静态访问方法_第2张图片

 我们就可以看到自己的放置的图片

你可能感兴趣的:(笔记)