本文介绍了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),它在家里运行nginx + wordpress的24 * 7.我还有一台笔记本电脑,每晚运行5个小时,运行Ubuntu.

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. 两个支持不同IP的客户端可以访问mysql,
  2. 对于RPi和Ubuntu,wordpress应该相同,这可以通过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有条件地反向代理或直接提供服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:26