nginx搭建文件系统随笔

导读:

在上云的时代,服务一般都会采用k8s集群部署服务应用,服务之间通过ceph进行共享数据。但是ceph存在很多问题,一旦ceph服务器卡住会导致共享ceph的业务服务器访问不了数据,并且只要卡住,一般恢复需要比较久的时间。会严重影响到业务。为此需要构建一个专门进行数据存储的服务,比如接入aws云,ali云或腾讯云的s3服务。为了便于与团队的业务进行数据交互,往往会本地服务器进行缓存数据,并且请求服务商的s3服务是需要收钱的。一般的逻辑为:用户向本地数据服务请求数据,本地数据服务查看本地是否有缓存(非最新数据认为没有本地缓存,需要重新下载),如果有直接返回数据的下载url,用户通过url进行实际的数据下载;若本地没有数据缓存或不是最新数据,则请求服务商的s3服务。把数据下载到本地,然后返回数据下载url给用户。本地的数据服务采用nginx进行管理,下面简单分享nginx搭建文件服务器的实践。

环境准备

在k8s部署的服务是容器服务,因此首先需要安装docker,下载基础镜像,安装nginx:

docker安装可参考 ,docker的基本操作可参考
安装nginx

本文介绍使用ubuntu18.04进行文件服务系统的搭建。ubuntu18.04系统的获取参考前面的docker操作指南即可。启动容器后,通过如下命令安装nginx方法一般有两种,一种是源码安装,一种是apt-get安装。

apt-get 安装
apt-get update
apt-get install nginx

使用这种方法安装的nginx在/usr/sbin/ngin下,nginx是一个可执行文件,cd到/usr/sbin目录下,输入./nginx命令即可启动nginx。也可以使用service ngxin start|reload|stop|status进行操作。nginx的默认配置在/etc/nginx/nginx.conf。

源码安装
1. 下载nginx源码
为了管理的方便,我们安装时使用--prefix
wget http://nginx.org/download/nginx-1.14.0.tar.gz
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx  # preix指定安装的目录,安装时可能会提示缺少一些依赖,按提示通过apt-get安装即可。
# 安装时若不通过prefix指定安装路径,则安装的东西会比较乱
	# 可执行文件默认放在 /usr/local/bin
	# 库文件默认放在 /usr/local/lib 
	# 配置文件默认放在 /usr/local/etc 
	# 其它的资源文件放在 /usr /local/share 

安装好之后,可以通过nginx -v查看nginx安装的版本。在/usr/local/nginx可以看到一下目录:
在这里插入图片描述
配置文件在conf/nginx.conf, 可执行文件在sbin/nginx, 源码安装的无法通过service ngxin start|reload|stop|status 进行操作,需要额外配置或通过可执行文件启动。

配置共享文件目录

默认的nginx.conf内容如下:

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

在server部分添加

location /share/ {  # for data download
     root  /data1/;
     autoindex on;
        }

这样配置的数据共享目录的实际目录为:/data1/share/*, 访问时输入ip:80/share/*可以下载对应的文件,比如在/data1/share/xx/data.tar.gz, 此时访问的地址为:ip:80/share/xx/data.tar.gz。

启动服务

使用apt-get安装的nginx启动

service ngxin start  # 若已经启动,可以通过service nginx reload更新配置

使用源码安装的ngxin,配置了service启动,则按上述方法启动即可,若没有配置service启动,则可以:

cd /usr/local/nginx/sbin/
./nginx
参考文献:

https://blog.csdn.net/qq_42693848/article/details/105395466
https://blog.csdn.net/qq_42693848/article/details/101153124

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值