CentOS安装Nginx-1.6.2+安全配置

注:以下所有操作均在CentOS 6.5 x86_64位系统下完成。

#准备工作#

在安装Nginx之前,请确保已经使用yum安装了pcre等基础组件,具体见《CentOS安装LNMP环境的基础组件》

然后创建www的用户组和用户,并且不允许登录权限:

# id www
id: www:无此用户
# groupadd www
# useradd -g www -s /sbin/nologin www
# id www
uid=501(www) gid=501(www) 组=501(www)

#Nginx的安装#

开始下载Nginx并进行编译安装:

# cd /usr/local/src
# wget http://nginx.org/download/nginx-1.6.2.tar.gz
# tar zxf nginx-1.6.2.tar.gz
# cd nginx-1.6.2
# ./configure --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module --with-http_gzip_static_module

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1: using OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/local/nginx-1.6.2"
  nginx binary file: "/usr/local/nginx-1.6.2/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx-1.6.2/conf"
  nginx configuration file: "/usr/local/nginx-1.6.2/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx-1.6.2/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx-1.6.2/logs/error.log"
  nginx http access log file: "/usr/local/nginx-1.6.2/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

# make && make install
# ln -s /usr/local/nginx-1.6.2/ /usr/local/nginx
# chown -R www:www /usr/local/nginx
# chown -R www:www /usr/local/nginx-1.6.2

把Nginx的sbin目录加入PATH:

# vim /etc/profile

export PATH=$PATH:/usr/local/mysql/bin:$JAVA_HOME/bin:/usr/local/nginx/sbin

# source /etc/profile

查看Nginx的版本信息,并且检验上一步骤是否成功:

# nginx -V
nginx version: nginx/1.6.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.6.2 --group=www --user=www --with-http_ssl_module --with-pcre --with-http_stub_status_module

至此,Nginx已经安装完毕。

#Nginx的启动/重启/关闭#

给Nginx的webapp配置相关路径(这里是为了后面运维管理方便,可以把不同的Web项目放到该目录下):

# mkdir -p /data/www

简单修改下配置文件:

# vim /usr/local/nginx/conf/nginx.conf

user  www;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile      on;
    keepalive_timeout  65;
    gzip  on;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

开始启动Nginx:

# nginx

这个时候打开浏览器访问地址http://youripaddress应该可以看到:

至此,Nginx已经启动成功。

一般来说,当修改了nginx.conf配置文件后,可以直接重启让配置生效,重启之前一般检测下配置文件是否正确:

# nginx -t
nginx: the configuration file /usr/local/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.6.2/conf/nginx.conf test is successful
# nginx -s reload

另外,重启也可以通过发信号的方式:

# kill -HUP ${master_pid}

关闭的命令如下:

# nginx -s quit
# nginx -s stop

注:quit表示等请求结束后再关闭,stop表示立刻关闭。

也可以通过发信号的方式来关闭:

# kill -QUIT ${nginx_master}
# kill -TERM ${nginx_master}
# kill -9 ${nginx_master}

注:-QUIT表示从容停止,等所有请求结束后再关闭进程;TERM则表示立刻关闭进程;-9表示强制关闭。

为了以后管理上的方便, 我们这里写个启动脚本,以后就可以用service命令来启动,如下:

# vim /etc/init.d/nginxd

#!/bin/sh
# chkconfig: 2345 85 15
# description:Nginx Server

NGINX_HOME=/usr/local/nginx-1.6.2
NGINX_SBIN=$NGINX_HOME/sbin/nginx
NGINX_CONF=$NGINX_HOME/conf/nginx.conf
NGINX_PID=$NGINX_HOME/logs/nginx.pid

NGINX_NAME="Nginx"

. /etc/rc.d/init.d/functions

if [ ! -f $NGINX_SBIN ]
then
    echo "$NGINX_NAME startup: $NGINX_SBIN not exists! "
    exit
fi

start() {
    $NGINX_SBIN -c $NGINX_CONF
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Starting $NGINX_NAME: " /bin/true
    else
        action $"Starting $NGINX_NAME: " /bin/false
    fi
}

stop() {
    kill `cat $NGINX_PID`
    ret=$?
    if [ $ret -eq 0 ]; then
        action $"Stopping $NGINX_NAME: " /bin/true
    else
        action $"Stopping $NGINX_NAME: " /bin/false
    fi
}

restart() {
    stop
    start
}

check() {
    $NGINX_SBIN -c $NGINX_CONF -t
}

reload() {
    kill -HUP `cat $NGINX_PID` && echo "reload success!"
}

relog() {
    kill -USR1 `cat $NGINX_PID` && echo "relog success!"
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    check|chk)
        check
        ;;
    status)
        status -p $NGINX_PID
        ;;
    reload)
        reload
        ;;
    relog)
        relog
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}"
        exit 1
esac

# chmod +x /etc/init.d/nginxd
# chkconfig nginxd on

这样子就可以通过service来启动:

# service nginxd start

#Nginx的安全配置#

1、 首先设置不允许目录浏览,默认配置即为不允许。

autoindex off

2、开启访问日志,nginx中默认已开启,这里我们后续为了运维管理上的方便最好把日志单独放到/data目录下。

access_log /data/www/logs/localhost.access.log

3、确保目录的安全,由于Nginx使用的是www用户启动,黑客入侵服务器成功后将获得www用户的权限,所以需要确保网站Web目录和文件的属主与启动用户不同,防止网站被黑客恶意篡改和删除。网站Web目录和文件的属主可以设置为root,其中Web目录权限统一设置为755,Web文件权限统一设置为644。只有上传目录等可读写权限的目录可以被设置为777,为了防止黑客上传木马到777权限目录中,还必须保证该777权限的目录没有执行脚本的权限。这里有两种情况处理:

1)对于使用PHP的业务,配置如下:

location ~* ^/data/www/logs/.*\.(php|php5)$ {
    deny all;
}

注:当然最安全的还是给PHP的可执行目录采用白名单的方式,这个我们在PHP的安装一节中再详细介绍。

2)对于非使用PHP的业务(如python、cgi等),则需要禁止外部访问777目录,配置如下:

location ~ ^/data/www/logs/ {
    deny all;
}

4、对于管理目录,需要限制访问的IP地址,比如这里限制访问nginx状态的:

server {
    location /nginx-admin {
        stub_status on;
        access_log logs/nginx-admin.log;
        allow 11.12.23.0/24;
        deny all;
    }

    location /admin {
        ...
    }
}

注:上面配置的11.12.23.0/24指的就是当前运维客户端的IP地址段。

在允许IP的机器上输入地址应该可以看到:

而不允许的用户访问应该是不可以的,会显示403错误,比如:

5、把Nginx默认的首页等页面删除,使用业务自己的首页来顶替。

6、不允许IP直接访问服务器,这样的好处是怕当IP地址泄漏出去之后,被人用别的域名来指向了这个IP地址,可以设置让其返回500等错误码。比如:

server {
    listen        80 default;
    return 500;
}
server {
    listen        80;
    server_name   www.tencent.com tencent.com;
    root          /data/www/tencent;

    access_log    /data/logs/nginx/tencent.access.log;
    error_log     /data/logs/nginx/tencent.error.log;
}

注:上面的配置表示当使用IP地址直接访问时将出错,而使用域名访问时(比如请求tencent.com则正常)。

时间: 2024-08-10 15:08:58

CentOS安装Nginx-1.6.2+安全配置的相关文章

centos安装nginx并配置SSL证书

centos安装nginx并配置SSL证书 安装nginx的命令 sudo yum install epel-release sudo yum install nginx 让nginx随系统启动而启动 sudo systemctl enable nginx 常用命令 启动:nginx 停止:nginx -s stop 重载配置:nginx -s reload 配置路径:/etc/nginx/ 日志路径:/var/log/nginx 打开配置文件 在HTTP节点下配置两个server节点,其他不变

centos 安装 nginx 及配置 的坑

centos 安装 nginx 教程 1.创建/etc/yum.repos.d/nginx.repo touch /etc/yum.repos.d/nginx.repo 2.编辑 sudo vim /etc/yum.repos.d/nginx.repo [nginx-stable]name=nginx stable repobaseurl=http://nginx.org/packages/centos/7/$basearch/gpgcheck=1enabled=1gpgkey=https://

centos 安装nginx 和php

1.官方下载地址:http://nginx.org/download/nginx-1.6.0.tar.gz 一:nginx 0. yum install gcc gcc-c++ yum install glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel yum -y install perl-devel perl-ExtUtils-Embed yum -y install gcc automake autoconf libtoo

Centos安装nginx服务

到http://nginx.org/en/download.html下载最新版本的Nginx并安装. 一 下载并安装pcre库ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ tar zxvf pcre-8.30.tar.gz ./configure     make    make install 二 安装openssl yum -y install openssl openssl-devel 三 下载tcp_proxy_module

centos安装nginx 带upstream

centos安装nginx 带upstream 用途:利用upstream进行socket数据中转各版本nginx下载地址:http://nginx.org/download/系统:CentOS 6.5 x64nginx版本:1.12.1安装方式:源码编译安装 1.安装必须环境,nginx的编译需要c++,同时prce(重定向支持)和openssl(https支持)也需要安装 yum install gcc-c++ yum -y install pcre* yum -y install open

centos安装Nginx,反向代理配置全过程

1.安装依赖 #gcc安装,nginx源码编译需要 yum install gcc-c++ #PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式 yum install -y pcre pcre-devel #zlib安装,nginx 使用zlib对http包的内容进行gzip yum install -y zlib zlib-devel #OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(

centos安装nginx

一.下载解压 官方下载地址:http://nginx.org/en/download.html 下载包:nginx-1.8.1.tar.gz,解压 tar xvf nginx-1.8.1.tar.gz 解压后的文件如下 二.安装依赖 nginx需要gcc.pcre.openssl.zlib依赖包,依次安装或更新 安装gcc编译器 yum install gcc 安装pcre库 yum install pcre* 安装openssl工具和库 yum install openssl* 安装zlib库

CentOS安装Nginx 实现HTTP代理

为了练手,在CentOS7上安装Nginx 实现HTTP代理功能 再加上认证功能 随手找了几篇教程,都是教你怎么自己编译的,其实直接去nginx.org看document,centos官方源有nginx的 于是, yum install nginx 安装完成.不知道路径?whereis nginx找一下,配置文件位置也知道了. 常用的命令: nginx -t 检查配置 nginx -s <signal> (signal=quit,reload) 实现HTTP代理,用到PROXY模块就好了,示例

centos 安装nginx + 多个tomcat负载均衡

今天在centos上安装了两个tomcat和nginx,进行配置.今天记录的只是最基本的实现测试.(不包含使用redis进行session共享) Nginx 是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.  其特点是占有内存少,并发能力强. 直接开始主题: 1,首先jdk应该是配好了我就不写了,安装nginx(我使用的rmp安装) ,安装 pcre 让nginx支持rewrite,我使用的是pcre2-10.00.t

RHEL/CentOS 安装 nginx

采用官方nginx源安装方法支持的环境 如果不支持,可以改为epel源 系统 版本 支持的平台 RHEL/CentOS 6.x x86_64, i386 RHEL/CentOS 7.4+ x86_64, ppc64le 也可以源码编译安装或直接yum安装.域名解析请提前设置好 1 配置nginx源 1.1 方法一:配置nginx官方源(推荐) [[email protected] ~]# vim /etc/yum.repos.d/nginx.repo 添加: [nginx] name=nginx