[Linux安全运维] Nginx相关

Nginx相关

1. 概述

Nginx是一种Web服务器,其具有高并发、高负荷的能力,具有以下优点:

  1. 稳定、系统资源消耗少、占用内存较少。
  2. 软件安装包小且定制化强。
  3. 具有高并发能力,可处理30000-50000个请求。

Nginx作为静态页面的web服务器,其主要考量其性能,非常注重效率。

配置文件(最基本的配置文件):

配置文件主要分为三个区块:全局块、events块和http块。

# 全局块
worker_processes  1;  
# 事件区块
events {
    worker_connections  1024;
}
# http区块
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost; #改为IP地址
        # 反向代理
        location / {
            root   html; #存放目录
            index  index.html index.htm;
        }
        # 错误页面路由
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

全局块:用于存放niginx服务器的整体配置,包括相关的进程数、进程ID、存放路径、日志存放路径、用户组等信息。

events块:用于存放niginx服务器和用户连接的相关配置。

http块:用于存放一些服务器访问控制和第三方配置,包括http全局块和server块。

2. 反向代理

2.1 反向代理相关

客户端访问时,不需要进行配置就可以进行访问。反向代理服务器会接收客户端的请求,然后反向代理服务器去选择访问,将获取的数据返回给客户端。这样的好处是,使用反向代理服务器可以隐藏真实服务器的IP,暴露的是代理服务器的IP

配置文件:

#接口端
location /police/ {
    proxy_pass   http://192.168.1.1:8852/police/;
    proxy_redirect default;
    proxy_http_version 1.1;
    proxy_connect_timeout   60;
    proxy_send_timeout      60;
    proxy_read_timeout      90;
}

如果遇到以/police 请求开头的接口,访问http://192.168.1.1:8852/police/。

若想要定义多个端口的反向代理,需要修改代理头(location /police/)和访问的IP地址(proxy_pass http://192.168.1.1:8852/police/;)。

2.2 正向代理

局域网中用户要访问Internet,通过代理服务器来访问服务器,则需要正向代理服务器。

3. 负载均衡

将请求从发送到单个服务器上变为发送到多个服务器上,由此实现负载均衡。

#动态服务器组
upstream dynamic_zuoyu {
    server localhost:8080;  #tomcat 7.0
    server localhost:8081;  #tomcat 8.0
    server localhost:8082;  #tomcat 8.5
    server localhost:8083;  #tomcat 9.0
    }

实现负载均衡有4种基本方法:轮询法、weight权重模式、ip_hash、least_conn。

方法 解释
轮询法 默认方式
weight权重 根据权重分配
ip_hash 依据ip分配
least_conn 依据最少连接时间分配

3.1 实现方式

  1. 轮询法(default):

    每个请求按照时间顺序逐一分配到各个服务器

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083;  #tomcat 9.0
        	# server 参数
        	#fail_timeout 最大失败时间
        	#max_fails	设置在fail_timeout参数设置的时间内最大失败次数,超过则认为停机
        	#fail_time	服务器会被认为停机的时间长度,默认为10s
        	#backup	标记该服务器为备用服务器,当主服务器停止时,请求会被发送到它这里
        	#down	标记服务器永久停机
    }
    
  2. weight权重(加权轮询):

    指定轮询机率,通过weight的权重来控制访问,访问比率和weight成正比。

    #动态服务器组
    upstream dynamic_zuoyu {
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082   backup;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }
    
  3. ip_hash:

    通过哈希算法处理请求,当用户再次访问某服务器时,会自动进行定位,每个请求按访问ip的hash结果进行分配,实现每个用户固定访问一个后端服务器。

    #动态服务器组
    upstream dynamic_zuoyu {
        ip_hash;    #保证每个访客固定访问一个后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082;  #tomcat 8.5
        server localhost:8083   max_fails=3 fail_timeout=20s;  #tomcat 9.0
    }
    
  4. least_conn:

    把请求转发给连接数较少的后端服务器。

    #动态服务器组
    upstream dynamic_zuoyu {
        least_conn;    #把请求转发给连接数较少的后端服务器
        server localhost:8080   weight=2;  #tomcat 7.0
        server localhost:8081;  #tomcat 8.0
        server localhost:8082 backup;  #tomcat 8.5
        server localhost:8083   max_fails=3	fail_timeout=20s;  #tomcat 9.0
    }
    

4. 动静分离

将动态页面与静态页面由不同的服务器来解析。

配置文件:

#访问静态资源服务器
location /image/ {
  root   /var/filecenter/;
}
location /static/ {
  root   /var/filecenter/;
}
location /car/ {
  root   /var/filecenter/;
}
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|js|css)$ {
  root /Users/dalaoyang/Downloads/static;
}

#动态页面访问后台服务
#接口端
        location /police/ {
            proxy_pass   http://192.168.1.1:8852/police/;
            proxy_redirect default;
            proxy_http_version 1.1;
            proxy_connect_timeout   60;
            proxy_send_timeout      60;
            proxy_read_timeout      90;
        }

5. 常用命令

yum install nginx	安装nginx
netstat -anput|grep nginx 查看nginx进程
netstat -nltp	查看服务器端口占用情况
cd /usr/local/nginx/sbin/
./nginx  启动
./nginx -s stop  停止
./nginx -s quit  安全退出
./nginx -s reload  重新加载配置文件  如果我们修改了配置文件,就需要重新加载。
ps aux|grep nginx  查看nginx进程

6. 完整配置文件

#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 {
    worker_connections  1024;
}


http {
    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        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;

        # redirect server error pages to the static page /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;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

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


    # 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;
    #    }
    #}

}
相关推荐
抛物线.2 分钟前
Kdump 原理分析及场景案例分析
linux·服务器·数据库
Magician_liu10 分钟前
Linux基础笔记分享(超详细~)
linux·运维·笔记
linux行者20 分钟前
elasticsearch教程
linux·运维·elk·elasticsearch
我叫白小猿34 分钟前
【日常记录-Docker】构建自定义MySQL镜像
运维·mysql·docker·容器·镜像
lfszejy1 小时前
探索四川财谷通抖音小店:安全与信赖的购物新体验
安全
kaixiang3001 小时前
ssh网络协议(服务名sshd,端口22)
运维·网络协议·ssh
obj-rainy1 小时前
PXE网络部署/服务器自动部署
linux·运维·网络
worker..1 小时前
[极客大挑战 2019]HardSQL1
安全
问今域中1 小时前
【知识点快读】DMA周期挪用
linux·运维·网络
伴野星辰2 小时前
怎么读取FRM、MYD、MYI数据文件
运维·数据库