编译安装nginx 1.16

准备源码包,并解压,创建nginx用户

[root@slave-master ~]# tar xf nginx-1.16.0.tar.gz
[root@slave-master ~]# useradd -r -s /sbin/nologin nginx
[root@slave-master ~]# cd nginx-1.16.0

 

准备开发包组

[root@slave-master nginx-1.16.0]# yum install gcc pcre-devel openssl-devel zlib-devel -y

 

开始编译安装

[root@slave-master nginx-1.16.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@slave-master nginx-1.16.0]# make -j 4 && make install

 

修改路径并启动

[root@slave-master nginx-1.16.0]# cd /apps/nginx/
[root@slave-master sbin]# ln -sv /apps/nginx/sbin/* /usr/sbin
[root@slave-master sbin]# nginx

 

 

基于basic认证,先修改主配置文件,让它包含一个目录,我们就可以单独写配置文件

[root@slave-master src]# vim /apps/nginx/conf/nginx.conf
http {
    include       mime.types;
    default_type  application/octet-stream;
    include /apps/nginx/conf/src/*.conf;


[root@slave-master src]# mkdir /apps/nginx/conf/src/
[root@slave-master src]# vim /apps/nginx/conf/src/nginx.conf
server {
        server_name www.magedu.com;
        location / {
        root /apps/nginx/conf/src/html;
        auth_basic "renzheng";
        auth_basic_user_file "/apps/nginx/conf/src/.nginx";
        }
}

 

利用htpasswd命令创建用户

[root@slave-master src]# htpasswd -c /apps/nginx/conf/src/.nginx bob
New password: 
Re-type new password: 

测试

 

实现status页面

server {
        server_name www.magedu.com;
        access_log /apps/nginx/conf/src/access.log main;
        location / {
        root /apps/nginx/conf/src/html;
        auth_basic "renzheng";
        auth_basic_user_file "/apps/nginx/conf/src/.nginx";
        }
        location = /nginx_status {
        stub_status;
        allow 127.0.0.1;
        deny all;
        }
}

 

 

基于JSON格式的访问日志

[root@slave-master src]# vim nginx.conf 
log_format access_json '{"@timestamp":"$time_iso8601",'
'"host":"$server_addr",'
'"clientip":"$remote_addr",'
'"size":$body_bytes_sent,'
'"responsetime":$request_time,'
'"upstreamtime":"$upstream_response_time",'
'"upstreamhost":"$upstream_addr",'
'"http_host":"$host",'
'"uri":"$uri",'
'"domain":"$host",'
'"xff":"$http_x_forwarded_for",'
'"referer":"$http_referer",'
'"tcp_xff":"$proxy_protocol_addr",'
'"http_user_agent":"$http_user_agent",'
'"status":"$status"}';

server {
        server_name www.magedu.com;
        access_log /apps/nginx/conf/src/access.log main;
        location / {
        root /apps/nginx/conf/src/html;
        auth_basic "renzheng";
        auth_basic_user_file "/apps/nginx/conf/src/.nginx";
        access_log /apps/nginx/conf/src/access_json.log access_json;

 

启用压缩功能

  
[root@slave-master messge]# vim ../nginx.conf 
 location /messge {
        root /apps/nginx/conf/src;
        gzip on;
        gzip_comp_level 6;
        gzip_min_length 64;
        gzip_vary on;
        gzip_types text/xml text/css application/javascript;
        }

 

如果客户请求的页面不存在将自动跳转至首页

[root@slave-master messge]# vim ../nginx.conf 

        if (!-f $request_filename) {
                rewrite (.*) http://www.magedu.com/index.html;
        }

 

ssl

[root@slave-master ssl]# vim ../nginx.conf
server {
        listen 443 ssl;
        server_name wwww.magedu.com;
        ssl_certificate     "/apps/nginx/conf/src/ssl/magedu.com.crt";
        ssl_certificate_key "/apps/nginx/conf/src/ssl/magedu.com.key";
        root "/apps/nginx/conf/src/jiami";
}

 

将http请求跳转至https

       

[root@slave-master ssl]# vim ../nginx.conf 
 if ($scheme = http) {
                rewrite / https://www.magedu.com/ redirect;
        }

 

防盗链

  
[root@slave-master ssl]# vim ../nginx.conf 
      valid_referers none blocked server_names *.magedu.com magedu.* www.magedu.org/galleries/ ~\.google\.;

        if ($invalid_referer) {
        return 403 "Forbidden Access";
}

 

配置文件下载

[root@slave-master src]# vim nginx.conf   
      location /download {
                root /apps/nginx/conf/src;
                autoindex on;
                autoindex_exact_size off;
                autoindex_localtime on;
                limit_rate 100k;
        }
autoindex on | off;
自动文件索引功能,默为off
autoindex_exact_size on | off;
计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on
autoindex_localtime on | off ;
显示本机时间而非GMT(格林威治)时间,默认off
autoindex_format html | xml | json | jsonp;
显示索引的页面文件风格,默认html

 

posted @ 2019-08-12 13:17  Linus小跟班  阅读(820)  评论(0编辑  收藏  举报