Ngine X搭建文件服务器

前言

Nginx是一种反向代理服务器
什么是反向代理服务器?
正向代理:指客户端通过下载特定服务器代理软件,将请求转发到代理服务器,再转发到接口服务器
反向代理:指服务端去使用软件使之扮演客户端角色,创建一个虚拟的服务器,把真正客户端的请求通过虚 拟服务器转发到接口服务器

所以说Nginx是安装在服务端的一种代理服务器

Nginx的安装

博主这里介绍的安装方法是在CentOS6.9基础上:
首先安装一些编译软件和指定库,CentOS原生系统不能完全编译Nginx

yum install gc gcc gcc-c++ pcre-devel zlib-devel openssl-devel

去官网把.tar.gz为后缀,稳定的Nginx版本下载到CentOS服务器上

编译并且安装:

#编译:
./configure --user=www --group=www --prefix=/usr/local/Nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
#安装:
make && make install

如此这般,Nginx在服务器上的部署已经完毕
安装的目录在/usr/local/Nginx路径下

为了操作方便,我们直接配置一些指定命令来启动和关闭Nginx:
第一步,打开init.d下的Nginx文件

vim /etc/init.d/Nginx

复制下面代码Nginx文件中,并且:wq保存:

#!/bin/bash  
# Nginx Startup script for the Nginx HTTP Server  
#  
# chkconfig: - 85 15  
# description: Nginx is a high-performance web and proxy server.  
# It has a lot of features,but it's not for everyone.  
# processname: Nginx  
# pidfile: /var/run/Nginx.pid  
# config: /usr/local/Nginx/conf/Nginx.conf  
Nginxd=/usr/local/Nginx/sbin/Nginx  
Nginx_config=/usr/local/Nginx/conf/Nginx.conf  
Nginx_pid=/usr/local/Nginx/Nginx.pid  
 
RETVAL=0  
prog="Nginx" 
 
# Source function library.  
. /etc/rc.d/init.d/functions  
 
# Source networking configuration.  
. /etc/sysconfig/network  
 
# Check that networking is up.  
[ ${NETWORKING} = "no" ] && exit 0  
 
[ -x $Nginxd ] || exit 0  
 
 
# Start Nginx daemons functions.  
start() {  
 
if [ -e $Nginx_pid ];then 
   echo "Nginx already running...." 
   exit 1  
fi  
 
   echo -n $"Starting $prog: " 
   daemon $Nginxd -c ${Nginx_config}  
   RETVAL=$?  
   echo  
   [ $RETVAL = 0 ] && touch /var/lock/subsys/Nginx  
   return $RETVAL  
 
}  
 
 
# Stop Nginx daemons functions.  
stop() {  
        echo -n $"Stopping $prog: " 
        killproc $Nginxd  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/Nginx /var/run/Nginx.pid  
}  
 
 
# reload Nginx service functions.  
reload() {  
 
    echo -n $"Reloading $prog: " 
 $Nginxd -s reload  
    #if your Nginx version is below 0.8,please use this command: "kill -HUP `cat ${Nginx_pid}`" 
    RETVAL=$?  
    echo  
 
}  
 
# See how we were called.  
case "$1" in 
start)  
        start  
        ;;  
 
stop)  
        stop  
        ;;  
 
reload)  
        reload  
        ;;  
 
restart)  
        stop  
        start  
        ;;  
 
status)  
        status $prog  
        RETVAL=$?  
        ;;  
*)  
        echo $"Usage: $prog {start|stop|restart|reload|status|help}" 
        exit 1  
esac  
 
exit $RETVAL

执行Nginx文件

chmod 755 /etc/init.d/Nginx

OK,先我们能使用service Nginx start/stop来启动/关闭Nginx

Nginx的使用

首先查看端口情况:

netstat -ntlp

看80端口是否被占用(阿里云的服务器请打开安全组):
如果80端口占用则杀死该进程,不想杀死进程,就在Nginx.conf修改Nginx认端口

Nginx的使用都在一个Nginx.conf的文件
如果你是按照我的安装方法安装,请打开目录:

/usr/local/Nginx/conf

目录下便会出现Nginx.conf文件,我们来看下文件中是什么(代码cat Nginx.conf):

解释下:
server表示这里新建了一个代理服务器
listen表示这个代理服务器监听的端口是8080(我这里改过,认是80端口)
server_name是代理服务器名(如果服务器有域名可以填写域名)
location才是我们真正需要自定义配置的地方:

root是我们需要代理的url路径
比如这里在location后面写了/
然后在root里面写了html
意思就是当我们在url中输入IP:端口/
Nginx会帮我们代理成IP:端口/usr/local/Nginx/html/

下面的index表示首页访问到index.html
因为/usr/local/Nginx/html/路径下Nginx在安装时创建了一个index.html(你可以打开目标路径看下)
所以上面截图中的server实际的操作结果就是:
访问IP:8080/index.html,会出现:

部署成文件服务器

我们指定服务器的一个端口,并且通过这个端口把服务器部署成文件服务器
依旧是在Nginx.conf文件下,用vi打开。
添加下列代码

server {
       listen 8079;
       server_name localhost;
       
       location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css|ttf|woff|woff2|zip)${
            root //picture;
            }
        }

很简单,我们新建一个名为localhost的代理服务器
它占用了8079这个端口
设置了可以读取的文件后缀名
并且这些文件是存储在centos根目录下的picture文件夹中

这里我做一个简单示例:

url:

页面展示:

以上便是用Nginx搭建一个简单的文件夹服务器
谢谢大家的阅读~记得点关注哦

相关文章

Centos下搭建性能监控Spotlight
CentOS 6.3下Strongswan搭建IPSec VPN
在CentOS6.5上安装Skype与QQ
阿里云基于centos6.5主机VPN配置
CentOS 6.3下配置multipah
CentOS安装、配置APR和tomcat-native