您的位置:首页 > 运维架构 > Nginx

nginx的安装&配置静态文件访问

2017-05-11 20:06 567 查看

安装Nginx

安装gcc g++依赖库

ubuntu平台可以使用如下命令

apt-get install build-essential
apt-get install libtool


centos平台可以使用如

//centos平台编译环境使用如下指令
//安装make:
yum -y install gcc automake autoconf libtool make

//安装g++:
yum install gcc gcc-c++ 装


安装 pcre依赖库(http://www.pcre.org/

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev


安装 zlib依赖库(http://www.zlib.net

apt-get install zlib1g-dev


安装 ssl依赖库

apt-get install openssl


安装Nginx(http://nginx.org

#下载最新版本:
wget http://nginx.org/download/nginx-1.11.3.tar.gz #解压:
tar -zxvf nginx-1.11.3.tar.gz
#进入解压目录:
cd nginx-1.11.3
#配置:
./configure --prefix=/usr/local/nginx
#编辑nginx:
make
#注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
#需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx
#启动nginx
/usr/local/nginx/sbin/nginx
./sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#停止nginx
./sbin/nginx -s stop
./sbin/nginx -s quit


配置静态文件访问

安装完nginx之后,进入nginx安装目录的conf目录下,修改nginx.conf文件,在一个server{}中添加 一个location 部分配置代码如下:

root@ubuntu:/usr/local/nginx/conf# vi nginx.conf
server {
listen       80;
server_name  localhost;
location / {
root   html;
index  index.html index.htm;
}
location /Image/ {
root   /opt/;
autoindex on;
}

#可配置多个目录访问
location /MessagePush/ {
root   /opt/;
autoindex on;
}
}


以上配置相当于访问http://localhost:80/Image/时,会访问到/opt/Image/目录下的文件,同理,访问http://localhost:80/MessagePush/时会到/opt/Message/目录下进行文件访问。





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nginx