Nginx系列

  1. 入门——Nginx系列——配置详解
  2. 进阶——Nginx系列——负载均衡配置
  3. 进阶——Nginx系列——解决跨域与接口可用性探测
  4. 进阶——Nginx系列——缓存解决接口性能问题
  5. 进阶——Nginx系列——accessLog日志挖掘与恶意IP封禁
  6. 进阶——Nginx系列——websocket反向代理与资源压缩


文章目录

  • Nginx系列
  • 1、什么是代理?
  • 2、配置文件与目录
  • 2.1、全局配置
  • 2.2、主机配置(server)
  • 2.3、localtion配置


1、什么是代理?

  1. 正向代理
    客户端和目标服务器之间服务器,客户端向代理发送一个请求指定目标服务器,然后代理向目标服务器请求回去内容,再将结果返回给客户
    核心——客户端知道目标服务器地址
  2. 反向代理
    客户端和目标服务器之间服务器,客户端向代理发送一个请求,然后代理向目标服务器请求回去内容,再将结果返回给客户
    核心——客户不知道目标服务器地址,由Nginx代理转发

2、配置文件与目录

nginx配置proxy_pass if语句 nginx配置详解proxy_负载均衡


nginx配置proxy_pass if语句 nginx配置详解proxy_负载均衡_02


命令:

vim  nginx.conf       或者    vi  nginx.conf
2.1、全局配置
# 每个配置项由配置指令和指令参数 2 个部分构成
#user nobody;             # 指定Nginx Worker进程运行以及用户组
worker_processes 1;  #  worker就是处理请求的,一般数量和CPU核心数一致

#error_log logs/error.log;            # 错误日志的存放路径 和错误日志对应日志的级别
#error_log logs/error.log notice;   
#error_log logs/error.log info;

#pid logs/nginx.pid;                    # 进程PID存放路径


# 事件模块指令,用来指定Nginx的IO模型,Nginx支持的有select、poll、kqueue、epoll 等。不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选
events { 
use epoll;
# 定义Nginx每个进程的最大连接数, 作为服务器来说: worker_connections * worker_processes,
# 作为反向代理来说,最大并发数量应该是worker_connections * worker_processes/2。因为反向代理服务器,每个 并发会建立与客户端的连接和与后端服务的连接,会占用两个连接
worker_connections 1024; 
}
2.2、主机配置(server)
# 虚拟主机的配置
server {
listen 80; # 虚拟主机的服务端口
server_name localhost; #用来指定IP地址或域名,多个域名之间用空格分开

#charset koi8-r;

#access_log logs/host.access.log main;

#URL地址匹配
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; //拦截PHP结尾的跳转到对应路径
#}

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

例如:我想要把localhost:8080的服务通过localhost:80访问 (当然你也可以使用域名)

server {
listen 80;                                    //监听端口
server_name  localhost;     //转发地址 可以填写你的域名

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass  http://localhost:8080;  //代理地址
}

#URL地址匹配
location / {
root html; # 服务默认启动目录
index index.html index.htm; #默认访问文件,按照顺序找
}

 # HTTPS server    //https 加密协议
    #
    #server {
    #    listen       443 ssl;   //监听端口
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;      //ssl证书秘钥地址
    #    ssl_certificate_key  cert.key;    //ssl证书公钥地址

    #    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.3、localtion配置
#URL地址匹配
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;
#}