设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 服务器 > Nginx

CentOS 7.3 下的安装 Nginx 提供 HTTP 服务

时间:12-14来源:作者:点击数:

参考了 nginx.org 的 安装文档。编辑 etc/yum.repos.d/nginx.repo:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1

然后执行:

$ yum update -y
$ yum install nginx -y
$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

配置 Nginx 以支持 WebDav

根据 whereis 得知 nginx 的配置文件位于 /etc/nginx。这个目录下有个 nginx.conf,是 nginx 的主配置文件。下级目录 conf.d 的所有 conf 扩展名的文件都会被 include 进入 nginx.conf。

编辑 conf.d/default.conf,添加以下内容:

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
      autoindex on;

      ## webdav config
      client_body_temp_path /tmp;
      dav_methods PUT DELETE MKCOL COPY MOVE;
      create_full_put_path  on;
      dav_access  group:rw  all:r;
  }

Nginx 的 root 目录 /usr/share/nginx/html 默认是只读的,而上述配置文件启用了 Nginx 的 webdav 模块 ngx_http_dav_module,这需要 root 目录的写权限,这样:

$ chmod 777 /usr/share/nginx/html

启动 Nginx:

$ nginx

下面还有两个 Nginx 常用命令,-T 是检测配置文件的语法,-s reload 是重新加载 Nginx 配置文件:

$ nginx -T
$ nginx -s reload

创建一个 t.txt 文件,并上传到 localhost 的根目录下(对应操作系统的 /usr/share/nginx/html):

$ echo "this is t.txt!" > t.txt
$ curl -T './t.txt' localhost
$ ll /usr/share/nginx/html    (可以看到t.txt上传到了这里)
$ curl localhost/t.txt

Kong 与 Nginx 并存

Kong 自带的 Nginx 的模块不全,没有 webdav 模块。可以象上面一样用 yum 安装 nginx。但启动 nginx 要小心,最好直接指定启动目录,否则很容易起来 Kong 自带的 Nginx。而且最好不要绑定默认的 80 端口,如改成 8002 端口。

编辑 Nginx 配置文件 /etc/nginx/conf.d/default.conf:

server {
  listen     8002;
  server_name  localhost;
  location / {
    root   /usr/share/nginx/html;
      autoindex on;
      ## webdav config
      client_body_temp_path /tmp;
      dav_methods PUT DELETE MKCOL COPY MOVE;
      create_full_put_path  on;
      dav_access  group:rw  all:r;
  }

启动 nginx 默认使用 /etc/nginx 下的配置文件:

$ chmod 777 /usr/share/nginx/html
$ /usr/sbin/nginx

然后测试一下 webdav 协议:

$ curl -T './t.txt' localhost:8002
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门
本栏推荐