Nginx部署、启动、停止、安全退出、重载配置文件、查看nginx进程、动静分离、黑白名单、防盗链

本文详细介绍了Nginx的安装步骤,包括下载、解压、编译及安装。接着讲解了如何配置Nginx,如动静分离、防盗链、黑名单设置等,并给出了配置示例。此外,还列举了Nginx的常用命令,如启动、停止、重新加载配置文件等。最后展示了Nginx配置文件`nginx.conf`的部分内容,包括http、server和location块的配置细节。
摘要由CSDN通过智能技术生成

一、安装

中文帮助文档

官方地址

复制下载地址,在linux执行

  • 进入下载目录
  • 下载:wget http://nginx.org/download/nginx-1.14.0.tar.gz
  • 解压:tar -zxvf nginx-1.14.0.tar.gz
  • cd nginx-1.14.0
  • 执行默认安装:./configure && make && make install
    • 基于参数构建 ./configure
    • 创建主文件 make
    • 执行完成之后 nginx 运行文件 就会被安装在 /usr/local/nginx 下
  • 查看配置文件
  • 选装特定场景安装:./configure --with-stream && make && make install
    在这里插入图片描述
  • 配置说明,更多配置请查看官网,或查看个人往期内容
    nginx基础信息介绍
    nginx编译
    nginx配置文件语法配置规则、热部署、日志切割
    nginx指令解释
    在这里插入图片描述
  • 启动
    • 进入文件中cd /usr/local/nginx/sbin/
    • 执行命令./nginx
  • 访问测试
    在这里插入图片描述

二、nginx常用命令

  • cd /usr/local/nginx/sbin/
  • ./nginx 启动
  • ./nginx -s stop 停止
  • ./nginx -s quit 安全退出
  • ./nginx -s reload 重新加载配置文件
  • ./nginx -t 验证配置文件是否正确
  • ps -ef|grep nginx 查看nginx进程

三、动静分离

  • 采用alias代理的方式
    location /old {
        # 别名转到/home/old_www/路径下同时忽略url中的old
        alias   /home/old_www/;
    }
    
  • 采用正则表达式的方式
    location ~ \.(js|css|png) {
        # 指定静态文件目录
        root /home/www/static/;
    }
    

四、防盗链

配置在location中

location / {
    # 转到/home/www/路径
    root   /home/www/;
    # 映射到index.html
    index  index.html;
    # 防盗链 判断引用页是否来源于*.codinganhour.com域名
    valid_referers none blocked *.codinganhour.com;
    if ($invalid_referer) {
        return 403;
    }
}

五、黑名单

配置在http下

http {
    # 黑名单
    # 封禁指定IP
    # deny 192.168.0.1;
    # allow 192.168.0.1;
    # 开放指定IP 段
    # allow 192.168.0.0/24;
    # 封禁所有
    # deny    all;
    # 开放所有
    # allow    all;
    # 引入 黑名单文件
    # include       black.ip;

六、nginx.config

# user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

# {}前的叫做 语句块,{ 前一定要有空格,否则错误,同时注意一定要在结尾加;
# 事件语句块
events {
    # 配置属性
    # 属性名            属性值:属性值可以有多个,如 默认location中index  index.html index.htm;
    # 当前最大连接数
    worker_connections  1024;
}


http {

    # 黑名单
    # 封禁指定IP
    # deny 192.168.0.1;
    # allow 192.168.0.1;
    # 开放指定IP 段
    # allow 192.168.0.0/24;
    # 封禁所有
    # deny    all;
    # 开放所有
    # allow    all;
    # 引入 黑名单文件
    # include       black.ip;

    # 把mine.types中的内容映射到当前文件中
    include       mime.types;
    # 响应类型
    default_type  application/octet-stream;

    # 日志输出格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    # 访问日志
    #access_log  logs/access.log  main;

    # 开启sendfile配置来提高文件的传输速率,跳过一些传输过程
    sendfile        on;
    # 只有在使用sendfile启用的情况下才能被允许,它可以配置一次发送数据包的大小。
    # 也就是说,数据包累积到一定大小后就发送。
    #tcp_nopush     on;
    # 长链接最大超时时间;
    keepalive_timeout  65;

    # gzip压缩
    #gzip  on;

    # 配置的具体站点 
    # 如果站点的listen与server_name
    #   都满足
    #       则优先匹配最大符合原则,左匹配大于右匹配,都相同的情况先匹配前面的
    #       
    # 例如一个server_name是www.baidu.com,另一个是*.baidu.com则优先匹配www.baidu.com站点
    # 例如一个server_name是*.baidu.com,另一个是www.baidu.*则优先匹配*.baidu.com站点
    #   都不满足
    #       则匹配第一个站点,或者监听端口后有default的站点
    #   listen       80 default;
    server {
        # 监听的端口,不同的站点可以配置相同的端口
        listen       80;
        # 站点的名称,可以配置多个中间用空格相隔,支持通配符的方式例如:*.baidu.com或www.baidu.*
        server_name  localhost;

        # 代理的字符编码
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 针对具体的目录划分,前缀匹配,正则匹配 =匹配

        # location的优先级如下
        # 1.=表示把URI作为字符串,以便与参数中的uri做完全匹配。
        # 2./ 基于uri目录匹配
        # 3.~表示正则匹配URI时是字母大小写敏感的。
        # 4.*表示正则匹配URI时忽略字母大小写问题。
        # 5.^~表示正则匹配URI时只需要其前半部分与uri参数匹配即可。

        # 匹配优先原则
        #1.精确匹配优先 =
        #2.正则匹配优先 ^~
        #3.前缀最大匹配优先。
        #4.配置靠前优化
        location / {
            # 站点的跟路径,root是nginx的安装目录  
            # 可以在server中配置也可以在location中配置,server中加,所有location共享
            root   html;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # 将服务器错误页面重定向到静态页面/50x.html
        error_page   500 502 503 504  /50x.html;
        # 语句块对应的参数
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    server {
        # 监听80端口
        listen       80;
        # 监听 www.codinganhour.com域名
        # 如果访问资源静态文件 需要/home/www/user/index.html存在这个文件,域名后必须是绝对路径  
        # http://www.codinganhour.com/user/index.html
        server_name  www.codinganhour.com www.coding.com;
        
        # 请求 http://www.codinganhour.com
        location / {
            # 转到/home/www/路径
            root   /home/www/;
            # 映射到index.html
            index  index.html;
            # 防盗链 判断引用页是否来源于*.codinganhour.com域名
            valid_referers none blocked *.codinganhour.com;
            if ($invalid_referer) {
                return 403;
            }
        }
        # 下载限速:
        location /download {
            # 限制每S下载速度
            limit_rate 1m; 
            # 超过30 之 后在下载
            limit_rate_after 30m; 
        }
        # 请求 http://www.codinganhour.com/old/index.html
        location /old {
            # 别名转到/home/old_www/路径下同时忽略url中的old
            alias   /home/old_www/;
            # 映射到index.html
            index  index.html;
        }

        # 请求 http://www.codinganhour.com/baidu 跳转到www.baidu.com
        # 如过http://www.baidu.com无/则请求的是www.baidu.com/old/baidu 
        #/则请求的是http://www.baidu.com
        location = /old/baidu {
            # 代理到指定url地址
            proxy_pass   http://www.baidu.com/;
        }   
        # 正则 大小写敏感.后缀是.js、.css、.png文件都转到csdn
        location ~ \.(js|css|png) {
            # 指定静态文件目录
            root /home/www/static/;
        }                
        # 正则 大小写不敏感.后缀是.js、.css、.png文件都转到csdn
        location ~ \.(js|css|png) {
            # 指定静态文件目录
            root /home/www/static/;
        }
    }

    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.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;
    #    }
    #}

}
  • 5
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值