
目录一、Nginx简单认识二、反向代理1.正向代理是什么2.反向代理是什么三、负载均衡、动静分离1.负载均衡2.动静分离四、Nginx在Linux系统下安装五、firewall开放端口\重启端口六、防火墙命令一、Nginx简单认识Nginx是一个高性能的HTTP和反向代理web服务器同时也提供IMAP/POP3/SMTP服务。是一款轻量级的web服务器/反向代理服务器及电子邮件IMAP/POP3代理服务器。特点占有内存少并发能力强其并发能力在同类的网页服务器中表现优秀。Nginx专为性能优化而开发性能是其重要的考量实现上非常注重效率能经受高负载的考验有报告表明能支持高达50,000个并发连接数。nginx做为HTTP服务器主要特性处理静态文件索引文件以及自动索引打开文件描述符缓冲。无缓存的反向代理加速简单的负载均衡和容错。FastCGI,简单的负载均衡和容错。Nginx支持热部署。它启动特别容易并且可以长时间不间断运行能够在不简单服务的情况下对软件版本进行升级。二、反向代理1.正向代理是什么简单说明在客户端浏览器配置代理服务器用户浏览网页时发起请求通过所配置的代理服务器进行互联网访问。2.反向代理是什么反向代理客户端对代理是无感知的因为不需要客户端对此进行任何配置。过程是我们只需将请求发送到反向代理服务器有反向代理服务器去选择目标服务器获取响应数据后再返回给客户端相对于客户端反向代理服务器和目标服务器是一个服务器暴露的是代理服务器地址隐藏的是真是服务器IP地址三、负载均衡、动静分离1.负载均衡当我们的应用服务的访问量够大单个服务器解决不了我们增加服务器的数量然后将请求分发到各个服务器上将原先请求集中到单个服务器的情况改为将请求分发到多个服务器上将负载分发到不同的服务器即负载均衡。2.动静分离为了加快网站的解析速度可以把动态页面和静态页面由不同的服务来解析加快解析速度降低单个服务器的压力。四、Nginx在Linux系统下安装1.使用远程连接工具xshell\finalshell连接linux操作系统2.下载并安装nginx安装所需的依赖包#首先切换到src目录下 [root bogon src]cd / usr / local / src /#包含make zlib openssl pcre等相关包 yum -y install make zlib zlib-devel gcc-c libtool openssl openssl-devel pcre yum install -y gcc make pcre-devel zlib-devel openssl-devel3.下载并安装nginx#cd / usr / local / src / # 下载最新稳定版 wget https://nginx.org/download/nginx-1.26.3.tar.gz tar -zxf nginx-1.26.3.tar.gz cd nginx-1.26.3 ./configure --prefix/usr/local/nginx \ --with-http_ssl_module \ --with-http_stub_status_module make make install#解压 tar -zxvf nginx-1.26.3.tar.gz#进入nginx目录 cd nginx-1.26.3 #./configure编译 ./configure #最后安装 makemake install安装成功之后在/usr中会多出来一个文件夹local/nginx,在nginx文件夹中有sbin文件里面有启动nginx的脚本cd /usr/local/nginx/4.启动nginx5.查看nginx进程ps -ef | grep nginx6.在nginx目录下有一个conf文件夹里面有一个nginx.conf里面作配置包括监听端口等#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 Apaches document root # concurs with nginxs one # #location ~ /\.ht { # deny all; #} } server { listen 18189; location /{ proxy_pass http://127.0.0.1:18081/; #设置ip转发 proxy_set_header Host $host; proxy_set_header X-real-ip $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /weChat/{ proxy_pass http://127.0.0.1:18085/; proxy_set_header Host $proxy_host; } location /file/{ proxy_pass http://127.0.0.1:7070/; proxy_set_header Host $proxy_host; } location /admin/{ proxy_pass http://127.0.0.1:17070/; proxy_set_header Host $proxy_host; } } server{ listen 3001; server_name localhost; location / { root aims-manage; try_files $uri $uri/ /index.html; } location ~ /api/ { proxy_pass http://127.0.0.1:18189; } } # 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; # } #} }五、firewall开放端口\重启端口7.在一开始默认端口并不是步骤6中的而是80端口我们需要在防火墙中开发80端口#查看开发的端口号 firewall-cmd --list-all #设置开放的端口号 firewall-cmd --add-servicehttp --permanent sudo firewall-cmd --add-port80/tcp --permanent #注意上面1句的端口号80就是你要打开的端口可以根据需要填写自己想要的端口号 #加上端口后重新防火墙 firewall-cmd --reload六、防火墙命令火墙的各类配置文件存储在/usr/lib/firewalld和/etc/firewalld/中的各种xml文件里 firewalld的操作 yum install firewalld firewall-config ##安装firewalld与图形界面 firewall-config ##打开图形界面 systemctl status firewalld ##查看火墙状态 systemctl start firewalld ##开启火墙服务 systemctl stop firewalld ##关闭火墙服务 systemctl enable firewalld ##开机自动开启 systemctl disable firewalld ##开机不自启 systemctl mask firewalld ##冻结火墙服务 systemctl unmask firewalld ##解冻火墙服务 firewall-cmd --state ##查看火墙的状态 firewall-cmd --get-default-zone ##查看火墙默认的域 firewall-cmd --get-active-zone ##查看火墙活动的域 firewall-cmd --get-zones ##查看火墙所有可用的域 firewall-cmd --zonepublic --list-all ##列出制定域的所有设置 firewall-cmd --get-services ##列出所有预设服务 firewall-cmd --list-all ##列出默认区域的设置 firewall-cmd --list-all-zones ##列出所有区域的设置 firewall-cmd --set-default-zonedmz ##设置默认区域为dmz firewall-cmd --add-source172.25.254.44 --zonetrusted ##添加172.25.254.44到trusted域中去 firewall-cmd --remove-source172.25.254.44 --zonetrusted ##删除172.25.254.44到trusted域中去 firewall-cmd --remove-interfaceeth1 --zonepublic ##删除public域中的eth1接口 firewall-cmd --add-interfaceeth1 --zonetrusted ##添加trusted域中一个接口eth1 firewall-cmd --add-servicehttp ##添加http服务到火墙中 firewall-cmd --add-port8080/tcp ##添加端口为8080协议为tcp的到火墙中 firewall-cmd --permanent --add-servicehttp ##永久添加http到火墙中 **-permanent参数表示永久生效设置如果没有指定-zone参数则加入默认区域 firewall-cmd --zonepublic --list-ports ##列出public域中端口 firewall-cmd --permanent --zonepublic --add-port8080/tcp ##添加端口 firewall-cmd --zonepublic --add-port80/tcp --permanent --permanent永久生效没有此参数重启后失效 firewall-cmd --permanent --zonepublic --remove-port8080/tcp ##删除端口 firewall-cmd --add-servicessh --permanent ##永久添加ssh服务添加完后重新加载一下就可以查看了 vim /etc/firewalld/zones/public.xml ##编写public域的配置文件,可以加服务本次实验添加lftp irewall-cmd -reload ##重新加载火墙不会立即中断当前使用的服务 firewall-cmd --complete-reload ##重新加载火墙会立即中断当前正在使用的服务 通过firewall-cmd 工具可以使用 --direct选项再运行时间里增加或移除链。如果不熟悉iptables,使用直接接口非常危险因为您可能无意间导致火墙被入侵。直接端口模式适用于服务或程序以便在运行时间内增加特定的火墙规则。直接端口模式添加的规则优先于应用。 firewall-cmd --direct --get-all-rules ##列出规则 firewall-cmd --direct --add-rule ipv4 filter INPUT 2 -s 172.25.254.44 -p tcp --dport 22 -j ACCEPT ##在filter表中的INPUT链中第二条加入允许接受tcp协议的172.25.254.44的数据包通过端口22sshd访问该主机 firewall-cmd --direct --remove-rule ipv4 filter INPUT 2 -s 172.25.254.44 -p tcp --dport 22 -j ACCEPT ##移除 firewall-cmd --direct --add-rule ipv4 filter INPUT 2 -s 172.25.254.44 -p tcp --dport 22 -j ACCEPT ##添加除了44主机以外的任何主机都可以访问 cat /etc/services | grep ssh ##查看与ssh有关的服务信息 ##端口转发地址伪装 firewall-cmd --add-forward-portport22:prototcp:toport22:toaddr172.25.254.44 ##别的主机通过22端口访问该主机的时候伪装到172.25.254.44主机上要开启伪装才可成功 firewall-cmd --permanent --add-masquerade ##开启伪装 firewall-cmd--reload ##需要重新加载 firewall-cmd --remove-forward-portport22:prototcp:toport22:toaddr172.25.254.44 ##移除 firewall-cmd --permanent --remove-masquerade ##关闭伪装 ##实现路由功能连接不同的ip进行地址伪装 在服务器上配两个网卡eth0:172.25.254.144 eth1:192.168.0.144 客户端192.168.0.244 firewall-cmd --add-rich-rulerule familyipv4 source address172.25.254.144 masquerade firewall-cmd --add-masquerade ##开启伪装 firewall-cmd --get-icmptypes firewall-cmd --add-icmp-blockdestination-unreacheable ##ping的时候显示目的地不可达 firewall-cmd --remove-icmp-blockdestination-unreacheable ##移除 firewall-cmd --add-icmp-blockecho_sed firewall-cmd --add-icmp-blockecho-request firewall-cmd --remove-icmp-blockecho-request firewall-cmd --add-icmp-blockecho-request --timeout5 ##