目录
一、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-dev pcre
3.下载并安装nginx
#cd / usr / local / src /
wget http://nginx.org/download/nginx-1.6.2.tar.gz
#解压
tar -zxvf nginx-1.6.2.tar.gz
#进入nginx目录
cd nginx-1.6.2
#./configure编译
./configure
#最后,安装
make&&make install
安装成功之后,在/usr中会多出来一个文件夹local/nginx,在nginx文件夹中有sbin文件,里面有启动nginx的脚本
cd /usr/local/nginx/
4.启动nginx
5.查看nginx进程
ps -ef | grep nginx
6.在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 Apache's document root
# concurs with nginx's 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-service=http --permanent
sudo firewall-cmd --add-port=80/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 --zone=public --list-all ##列出制定域的所有设置
firewall-cmd --get-services ##列出所有预设服务
firewall-cmd --list-all ##列出默认区域的设置
firewall-cmd --list-all-zones ##列出所有区域的设置
firewall-cmd --set-default-zone=dmz ##设置默认区域为dmz
firewall-cmd --add-source=172.25.254.44 --zone=trusted ##添加172.25.254.44到trusted域中去
firewall-cmd --remove-source=172.25.254.44 --zone=trusted ##删除172.25.254.44到trusted域中去
firewall-cmd --remove-interface=eth1 --zone=public ##删除public域中的eth1接口
firewall-cmd --add-interface=eth1 --zone=trusted ##添加trusted域中一个接口eth1
firewall-cmd --add-service=http ##添加http服务到火墙中
firewall-cmd --add-port=8080/tcp ##添加端口为8080,协议为tcp的到火墙中
firewall-cmd --permanent --add-service=http ##永久添加http到火墙中
**-permanent参数表示永久生效设置,如果没有指定-zone参数,则加入默认区域
firewall-cmd --zone=public --list-ports ##列出public域中端口
firewall-cmd --permanent --zone=public --add-port=8080/tcp ##添加端口
firewall-cmd --zone=public --add-port=80/tcp --permanent (--permanent永久生效,没有此参数重启后失效)
firewall-cmd --permanent --zone=public --remove-port=8080/tcp ##删除端口
firewall-cmd --add-service=ssh --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的数据包通过端口22(sshd)访问该主机
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-port=port=22:proto=tcp:toport=22:toaddr=172.25.254.44 ##别的主机通过22端口访问该主机的时候伪装到172.25.254.44主机上(要开启伪装才可成功)
firewall-cmd --permanent --add-masquerade ##开启伪装
firewall-cmd--reload ##需要重新加载
firewall-cmd --remove-forward-port=port=22:proto=tcp:toport=22:toaddr=172.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-rule="rule family=ipv4 source address=172.25.254.144 masquerade"
firewall-cmd --add-masquerade ##开启伪装
firewall-cmd --get-icmptypes
firewall-cmd --add-icmp-block=destination-unreacheable ##ping的时候显示目的地不可达
firewall-cmd --remove-icmp-block=destination-unreacheable ##移除
firewall-cmd --add-icmp-block=echo_sed
firewall-cmd --add-icmp-block=echo-request
firewall-cmd --remove-icmp-block=echo-request
firewall-cmd --add-icmp-block=echo-request --timeout=5 ##