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

nginx的站点安全

2015-08-27 21:25 477 查看
版权声明:Copyright 却邪水心,转载请说明出处 https://blog.csdn.net/qq_37247293/article/details/88564328

1、源代码安装nginx

    1)、所需环境:

            开发环境:Development tools

                      Server Platform Development

                      Additional Development

                      pcre-devel

            nginx-1.6.0.tar.gz


    2)、源码安装nginx:

            拆解源代码包到/usr/local/src/目录下

            创建系统账号和组nginx[root@fcy ~]# groupadd -r nginx [root@fcy ~]# useradd -r -g nginx nginx             编译源代码:
[root@fcy nginx-1.6.0]# ./configure --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi --with-pcre            安装nginx:[root@fcy nginx-1.6.0]# make &&make install            执行nginx的测试命令,检测nginx的安装
[root@fcy nginx]# /usr/local/nginx/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory) nginx: configuration file /etc/nginx/nginx.conf test failed            提示缺少目录,创建即可:
[root@fcy nginx]# mkdir -p /var/tmp/nginx/client [root@fcy nginx]# ll /var/tmp/nginx/client/ total 0            启动nginx并查看进程和端口号:

            输入IP地址测试nginx是否可用:

2、实现基于IP地址的访问

        添加两个测试页面tec和mkt
[root@fcy nginx]# echo "tec page test">html/tec/index.html [root@fcy nginx]# echo "tec page test">html/mkt/index.html

        给网卡添加两个虚拟地址:

        

        修改nginx的配置文件,添加相关设置:


        重新加载配置文件,并添加地址解析:
[root@fcy nginx]# /usr/local/nginx/sbin/nginx -s reload        


        测试:


3、基于虚拟目录的别名访问

        在根目录/ 下有一个站点名称为qazwsxedc,我希望在访问时不适用这么难以记忆的名称,而是更为熟悉的vhost,即可以使用nginx的别名设置

        创建目录/qazwsxedc,并设置主页index.htnl,内容为qazwsxedc page test:
[root@fcy nginx]# mkdir /qazwsxedc [root@fcy nginx]# echo "qazwsxedc page test">/qazwsxedc/index.html        修改配置文件,添加别名记录:

        重载配置,并访问测试:
[root@fcy nginx]# /usr/local/nginx/sbin/nginx -s reload        


4、nginx的来源控制

    设置允许192.168.47.200的IP地址访问,拒绝192.168.47.150访问

    写在server下表示控制访问主站点的来源,也可以写在location下,表示针对某个站点


    重载并访问测试:



5、nginx的身份验证

    修改配置文件,添加认证设置:

    使用指令htpasswd产生认证文件.htpasswd(该指令包含在httpd-tools中)

    

    重载并访问测试:


6、nginx的加密访问

    结合ssl实现数据的加密访问# cd /etc/nginx/cert # openssl genrsa -des3 -out nginx.key 1024 # openssl req -new -key nginx.key -out nginx.csr # cp nginx.key nginx.key.bak # openssl rsa -in nginx.key.bak -out nginx.key # openssl x509 -req -days 365 -in nginx.csr -signkey nginx.key -out nginx.crt    修改nginx配置文件,添加ssl选项:

    

    重载nginx并查看端口

    访问测试:

    

        安装该证书:


访问成功:


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