系列文章目录
` nginx之配置文件服务器
文章目录
前言
对于我们linux开发者来说,尤其是嵌入式的开发者,常常会出现这样的场景,在编译机上把程序的bin或库编译好后,需要把它拷贝到我们的目标机器上执行,常用的方法有samba、ftp文件传输,但这些都是比较麻烦的,下面来介绍一下在编译机上安装nginx并配置文件服务器,在目标机上使用wget命令将需要的bin文件或库文件快速拉取到目标机执行的方法。
一、nginx的下载与安装
1.1 apt-get安装nginx
# 安装
sudo apt-get install nginx
# 查看nginx是否安装成功
nginx -v
# 启动nginx
sudo service nginx start
#启动后,在网页重输入ip地址,即可看到nginx的欢迎页面。至此nginx安装成功
# nginx文件安装完成之后的文件位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
# 卸载apt-get安装的nginx
# 彻底卸载nginx
sudo apt-get --purge autoremove nginx
#查看nginx的版本号
nginx -v
1.2 下载nginx包安装
# 安装依赖包
sudo apt-get install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g zlib1g-dev
sudo apt-get install openssl openssl-dev
sudo apt-get install openssl
sudo apt-get install libssl-dev
# 安装nginx
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.20.2.tar.gz
tar -xvf nginx-1.20.2.tar.gz
# 编译nginx
# 进入nginx目录
/usr/local/nginx/nginx-1.20.2
# 执行命令
./configure
# 执行make命令
make
# 执行make install命令
make install
#进入nginx启动目录
cd /usr/local/nginx/sbin
# 启动nginx
./nginx
#网页输入ip地址,访问成功,到此,nginx安装完毕
二、nginx配置简单文件服务器
2.1 修改配置文件
打开nginx的配置文件 /etc/nginx/nginx.conf,增加如下几行:
用于文件共享目录的访问权限,nginx默认是nobody的,如果不进行用户设置,进行wget时会提示403错误,所以我将用户配置成了启动nginx的用户名,比如我的用户名是user_1
user user_1; # 启动nginx的用户
日志输出:
error_log /var/log/nginx/error.log info; # nginx的日志输出
文件服务器的配置,其中root /home/user_1/file-server 这一行是配置文件共享的路径,把你需要被wget的文件放到这个目录下。
# 文件服务器server配置
server {
listen 8082; # 监听端口
server_name localhost;
root /home/user_1/file-server; # 文件共享目录
autoindex on;
location / {
root /home/user_1/file-server; # 文件共享目录
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
}
add_header Cache-Control "no-cache,must-revalidate";
}
增加后的nginx.conf文件如下:
#user nobody;
user user_1; # 启动nginx的用户
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx/error.log info; # nginx的日志输出
#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配置
server {
listen 8082;
server_name localhost;
root /home/user_1/file-server; # 文件共享目录
autoindex on;
location / {
root /home/user_1/file-server; # 文件共享目录
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
}
add_header Cache-Control "no-cache,must-revalidate";
}
server {
listen 8080;
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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include servers/*;
}
配置完成后,退出保存。
2.2 重启nginx
service nginx restart
2.3 验证
在/home/user_1/file-server目录下创建 a.txt文件,然后在目录机执行 wget http://ip:8982/a.txt ,便可以将文件a.txt从你的编译机下载到目标机。
总结
`以上就是今天要分享的内容,本文仅仅简单介绍了nginx的下载安装与配置简单文件服务器的方法,而nginx提供的功能远不如此,后续希望能学习到更多的知识与大家分享,也欢迎大家对我说得不对的地方,提出建议,谢谢大家。