ngixn 有条件地反向代理或直接服务 [英] ngixn conditionally reverse proxy or serve directly

查看:14
本文介绍了ngixn 有条件地反向代理或直接服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是询问是否有可能使 nginx 有条件地将请求重定向到其他服务器(通过反向代理)或自行处理请求.

The question is to ask for a possibility of making nginx conditionally redirect requests to other servers (by reverse proxy) or process the request by itself.

这是详细信息.

我有一个 Raspberry Pi (RPi),在家里以 24*7 的速度运行 nginx + wordpress.我还有一台笔记本电脑,每晚运行 Ubuntu 大约 5 个小时.

I have a Raspberry Pi (RPi) running nginx + wordpress for 24*7 at home. I also have a laptop running Ubuntu for about 5 hours every night.

RPi 上的 wordpress 运行良好,但速度很慢(尤其是在 php 上运行时).所以我想让笔记本电脑帮忙:

The wordpress on RPi is working great but it's slow (especially when it's working on php). So I would like to let the laptop help:

  • 如果笔记本电脑打开,RPi 的 nginx 通过反向代理将所有请求重定向到 Ubuntu;
  • 如果笔记本电脑关闭,RPi 的 nginx 会照常处理请求.

我想知道是否有可能实现这一目标?如果是,如何配置RPi和Ubuntu?

I wonder if it's possible to achieve this? If yes, how to configure RPi and Ubuntu?

推荐答案

基本的解决方案是,将 nginx 作为带有 fail_timout 的反向代理,当它收到请求时,它会分派到 Ubuntu 优先级更高的上游,并且如果 Ubuntu 离线,RPi 会自行处理请求.

The basic solution is, make nginx as a reverse-proxy with fail_timout, when it receives a request, it dispatch to the upstreams where Ubuntu has higher priority, and if Ubuntu is offline, RPi will handle the request by itself.

这需要:

  1. mysql可以被两个不同ip的客户端访问,已经支持;
  2. wordpress 对于 RPi 和 Ubuntu 应该是一样的,这可以通过 nfs share 来完成;
  3. nginx 应该正确配置.

以下是配置的详细信息.

Below is the details of configuration.

注意,在我的配置中:

  • RPi 的 IP 为 192.168.1.100,Ubuntu 的 IP 为 192.168.1.101;
  • wordpress 只允许 https,所有 http 请求都重定向到 https;
  • 服务器监听 80 和 443 端口,上游监听 8000 端口;

Mysql

/etc/mysql/my.cnf中设置bind-address = 192.168.1.100,并确保skip-networking不是定义;

Set bind-address = 192.168.1.100 in /etc/mysql/my.cnf, and make sure skip-networking is not defined;

在 mysql 的控制台中授予 RPi 和 Ubuntu 权限:

Grant permission to RPi and Ubuntu in mysql's console:

grant all on minewpdb.* to 'mineblog'@'192.168.1.100' identified by 'xxx';
grant all on minewpdb.* to 'mineblog'@'192.168.1.100' identified by 'xxx';

WordPress

正确设置 DB_HOST:

Set DB_HOST correctly:

define('DB_NAME', 'minewpdb');
define('DB_USER', 'mineblog');
define('DB_PASSWORD', 'xxx');
define('DB_HOST', '192.168.1.100');

NFS

在RPi上,安装nfs-kernel-server,通过/etc/exports

On RPi, install nfs-kernel-server, and export by /etc/exports

/path/to/wordpress 192.168.1.101(rw,no_root_squash,insecure,sync,no_subtree_check)

要在 RPi 上启用 nfs 服务器,还需要 rpcbind:

To enable nfs server on RPi, rpcbind is also required:

sudo service rpcbind start
sudo update-rc.d rpcbind enable
sudo service nfs-kernel-server start

在 Ubuntu 上,挂载 nfs(它也应该在/etc/fstab 中设置以使其自动挂载)

On Ubuntu, mount the nfs (it should also be set in /etc/fstab to make it mount automatically)

sudo mount -t nfs 192.168.1.100:/path/to/wordpress /path/to/wordpress

Nginx

在 RPi 上,创建一个新的配置文件 /etc/nginx/sites-available/wordpress-load-balance,包含以下参数:

On RPi, make a new config file /etc/nginx/sites-available/wordpress-load-balance, with below parameters:

upstream php {
  server unix:/var/run/php5-fpm.sock;
}

upstream mineservers {
  # upstreams, Ubuntu has much higher priority
  server 192.168.1.101:8000   weight=999 fail_timeout=5s max_fails=1;
  server 192.168.1.100:8000;
}

server {
  listen 80;
  server_name mine260309.me;
  rewrite     ^ https://$server_name$request_uri? permanent;
}

server {
  listen          443 ssl;
  server_name     mine260309.me;

  ssl_certificate     /path/to/cert/cert_file;
  ssl_certificate_key /path/to/cert/cert_key_file;
  ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers         HIGH:!aNULL:!MD5;
  access_log         /path/to/wordpress/logs/proxy.log;
  error_log            /path/to/wordpress/logs/proxy_error.log;

location / {
  # reverse-proxy to upstreams
  proxy_pass  http://mineservers;

  ### force timeouts if one of backend is died ##
  proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;

  ### Set headers ####
  proxy_set_header        Accept-Encoding   "";
  proxy_set_header        Host            $host;
  proxy_set_header        X-Real-IP       $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

  ### Most PHP, Python, Rails, Java App can use this header ###
  #proxy_set_header X-Forwarded-Proto https;##
  #This is better##
  proxy_set_header        X-Forwarded-Proto $scheme;
  add_header              Front-End-Https   on;

  ### By default we don't want to redirect it ####
  proxy_redirect     off;
  }
}

server {
  root /path/to/wordpress;
  listen          8000;
  server_name     mine260309.me;
  ... # normal wordpress configurations
}

在 Ubuntu 上,它可以使用相同的配置文件.

On Ubuntu, it can use the same config file.

现在,RPi 的 nginx 服务器在端口 443 上收到的任何请求,都会被分派到 Ubuntu 或 RPi 的端口 8000,其中 Ubuntu 具有更高的优先级.如果 Ubuntu 离线,RPi 本身也可以处理请求.

Now any request received by RPi's nginx server on port 443, it's dispatched to either Ubuntu or RPi's port 8000, where Ubuntu has much higher priority. If Ubuntu is offline, RPi itself can handle the request as well.

欢迎任何评论!

这篇关于ngixn 有条件地反向代理或直接服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆