配置nginx图片路径,访问nginx上的图片

碰到的问题:在做一个页面的时候,想给页面加一个图标,但是不能是本地的资源,只能是在线上的图标,又想用自己的图标,哈哈。

1、连接Linux服务器

我服务器用的是ubuntu 16.04系统,确保服务器开放了22端口和ssh服务,然后用putty连接服务器用来输入命令,登入系统。
输入sudo vim /etc/nginx/sites-available/default
打开nginx配置文件:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    # Add index.php to the list if you are using PHP

    server_name _;

    location / {
                root /var/www/html;
                index index.htm index.nginx-debian.html;
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #   include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
    #   # With php7.0-fpm:
    #   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #   deny all;
    #}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#

然后把图片的配置加在最后:

location ~ .*\.(gif|jpg|jpeg|png)$ {  
    expires 24h;  
      root /var/www/html/;#指定图片存放路径  
      access_log /usr/local/websrv/nginx-1.9.4/logs/images.log;#日志存放路径  
      proxy_store on;  
      proxy_store_access user:rw group:rw all:rw;  
      proxy_temp_path     /var/www/html;#图片访问路径  
      proxy_redirect     off;  
      proxy_set_header    Host 127.0.0.1;  
      client_max_body_size  10m;  
      client_body_buffer_size 1280k;  
      proxy_connect_timeout  900;  
      proxy_send_timeout   900;  
      proxy_read_timeout   900;  
      proxy_buffer_size    40k;  
      proxy_buffers      40 320k;  
      proxy_busy_buffers_size 640k;  
      proxy_temp_file_write_size 640k;  
      if ( !-e $request_filename)  
      {  
         proxy_pass http://127.0.0.1;#默认80端口  
      }  
  }  

当然你的图标要已经存在那个文件夹里了。

我用的是winSCP文件传输:

这里写图片描述
其实也可以在这里直接编辑上面那个nginx的配置文件,不需要putty,哈哈
这里写图片描述
然后这时候打开网址:
这里写图片描述
000.000.000.000用自己服务器的公网ip替换就行了

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
使用Nginx配置访问图片路径和静态页面非常简单。以下是一个示例配置文件的示例: ``` server { listen 80; server_name yourdomain.com; root /path/to/your/website; location /images { alias /path/to/your/images; autoindex on; } location /static { alias /path/to/your/static/files; autoindex on; } location / { try_files $uri $uri/ /index.html; } } ``` 在上述配置文件中,我们定义了一个虚拟主机,监听80端口,并使用`yourdomain.com`作为服务器名。 - 配置访问图片路径:使用`location /images`指令来配置访问图片路径。将`/path/to/your/images`替换为实际存储图片的目录路径。如果需要显示图片列表,可以使用`autoindex on`指令。 - 配置访问静态页面:使用`location /static`指令来配置访问静态页面的路径。将`/path/to/your/static/files`替换为实际存储静态页面的目录路径。同样,如果需要显示文件列表,可以使用`autoindex on`指令。 - 其他路径:对于其他路径,我们使用`location /`指令,并使用`try_files`指令来尝试查找匹配的URI,如果找不到则返回`index.html`。你可以将`index.html`替换为你的默认页面。 请确保在配置文件中替换实际的路径和域名,然后使用`nginx -s reload`命令重新加载配置文件,以使配置生效。 以上配置允许通过`yourdomain.com/images`访问图片文件和列表,通过`yourdomain.com/static`访问静态页面和文件,还可以通过`yourdomain.com`访问默认页面或其他匹配的页面。 希望以上信息能够帮助到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值