码迷,mamicode.com
首页 > 其他好文 > 详细

nginx配置文件

时间:2018-09-03 10:04:11      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:status   driver   day   ofo   建议   sig   ndk   .com   open   

1.访问控制
vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
allow 192.168.56.1; //添加此行
deny all; //添加此行
}

技术分享图片

location / {
root html;
index index.html index.htm;
deny 192.168.56.1; //修改此行
allow all; //修改此行
}

技术分享图片

2.基于用户认证
[root@hyj ~]# yum provides htpasswd
[root@hyj ~]# yum install httpd-tools -y
[root@hyj ~]# cd /usr/local/nginx/
[root@hyj nginx]# mkdir auth

这里的密码为加密后的密码串,建议用htpasswd来创建此文件
[root@hyj ~]# htpasswd -c -m /usr/local/nginx/auth/.user_auth_file ranran
New password:
Re-type new password:

user_auth_file内容格式为:username:password
[root@hyj ~]# cat /usr/local/nginx/auth/.user_auth_file
ranran:$apr1$U4iGIk.V$/OqSCBKpXLLOG39cZvQwy.

auth_basic "欢迎信息";
auth_basic_user_file "/path/to/user_auth_file"
编辑配置文件:
[root@hyj ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "i love you,ran"; //添加此行
auth_basic_user_file /usr/local/nginx/auth/.user_auth_file; //添加此行
}
输入ip192.168.56.12访问

技术分享图片

//用设置的用户名,密码登录

技术分享图片

3.https配置
openssl实现私有CA
CA的配置文件:/etc/pki/tls/openssl.cnf
a)CA生成一对密钥
[root@hyj ~]# cd /etc/pki/CA
[root@hyj CA]# (umask 077;openssl genrsa -out private/cakey.pem 2048) //生成密钥,括号必须要
Generating RSA private key, 2048 bit long modulus
................................................................................................................+++
..................+++
e is 65537 (0x10001)
[root@hyj CA]# openssl rsa -in private/cakey.pem -pubout //提取公钥
writing RSA key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAz89y+Qh4cK+YSCZJd7Mc
LnLkBgHGy4HKdwMHHoCfBi+EE9LEMF3WqQp8Q0BEsqNDknUVyK2Owg+sVFvdwgBF
nCz2zRN9Hp8r29ysZ6EUVCiVWj1ka5byMUxwEPJA1dt8F+o6qaYaBXe5JAzA9OoK
OdtN6oc1yLGwdpxSNpJkCGZnam9Xl/PTuhLt0z1LCsz+wGhVMX8kEg1tSXbUEMMK
Bfd7kaNKMUHh7lohNMZ25+4YxOJIjrvB3sc+hFuZMTI93ip4qPHoqaNkSQIq/cvJ
e08XYbjrwz1Y414g+LbbFzYtcC1asNreCUTHWiX3IivTuL/ScqKrAH5VxWCALwVn
dQIDAQAB
-----END PUBLIC KEY-----

b)CA生成自签署证书
//生成证书
[root@hyj CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
//读取证书内容
[root@hyj CA]# openssl x509 -text -in cacert.pem
[root@hyj CA]# mkdir certs newcerts crl
[root@hyj CA]# touch index.txt && echo 01 > serial
c)客户端(例如nginx服务器)生成密钥
[root@hyj ~]# cd /usr/local/nginx && mkdir ssl && cd ssl
[root@hyj ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
d)客户端生成证书签署请求
[root@hyj ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
[root@hyj ssl]# openssl ca -in ./nginx.csr -out nginx.crt -days 365
Certificate is to be certified until Sep 2 18:20:40 2019 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated

//编辑配置文件
[root@hyj ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 443 ssl;
server_name www.ranran520.com; //编辑此处,用域名

    ssl_certificate /usr/local/nginx/ssl/nginx.crt;     //编辑此处
    ssl_certificate_key /usr/local/nginx/ssl/nginx.key;   编辑此处

    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;

    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    location / {
        root html;
        index index.html index.htm;
    }
}

[root@hyj ~]# nginx -t //检查是否有语法错误
[root@hyj ~]# nginx -s reload //重新加载配置文件

修改C:\Windows\System32\drivers\etc下面的hosts文件,添加域名映射,可实现域名访问
192.168.56.12 www.ranran520.com

技术分享图片

4.开启状态界面‘
开启status
location /status {
stub_status on;
allow 192.168.56.1;
deny all;
}

技术分享图片

5.rewrite
[root@hyj ~]# cd /usr/local/nginx/
[root@hyj nginx]# cd html/
[root@hyj html]# mkdir images
//传张图片到images目录下,用于验证
[root@hyj images]# ls
1.jpg
//修改配置文件,
location / {
root html;
index index.html index.htm;
}
//添加以下内容
location /images {
root html;
index index.html;
}
//访问

技术分享图片

//将images目录重命名
[root@hyj html]# ls
50x.html images index.html
[root@hyj html]# mv images imgs
[root@hyj html]# ls
50x.html imgs index.html
//修改配置文件
location /images {
root html;
index index.html;
rewrite ^/images/(.*.jpg)$ /imgs/$1 break; //添加此行,将原先images的内容赋予imgs
}
//用原先的url访问

技术分享图片

rewrite ^/images/(.*.jpg)$ http://www.baidu.com;
//将原先的images链接到百度,用192.168.56.12/images/1.jpg访问

技术分享图片

nginx配置文件

标签:status   driver   day   ofo   建议   sig   ndk   .com   open   

原文地址:http://blog.51cto.com/13729085/2169256

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!