用于激活nginx配置/网站的小型服务

时间:2015-12-28 21:32:49

标签: python bash nginx uwsgi

我有一个备有冷安装(未运行)网站的备份服务器。

这些网站也位于多个生产服务器上,有时其中一些服务器也会停机。我为生产服务器配置了DNS后备,因此如果出现问题,请求将被发送到我的备份服务器,在那里使用nginx + uwsgi处理它们。

当请求被发送到备份服务器时,nginx使用默认的nginx配置(/etc/nginx/conf.d/default_site.conf)

(因为没有运行与网址匹配的网站,因为/ etc / nginx / sites-enabled中没有符号链接

注意:我在/ etc / nginx / sites-available和/ etc / uwsgi / apps-available中有所有网站的nginx和uwsgi配置。

我想编写一个Python(或Perl或bash脚本),它将:

每当请求默认的nginx配置时:

  1. 检查网站中是否存在匹配的nginx配置以及应用中可用的匹配uwsgi进程。如果是这样的话:
  2. 在启用应用的情况下创建指向uwsgi记录的链接并启动服务
  3. 在启用网站的地址中创建nginx记录的链接,然后重新启动nginx
  4. 这是我到目前为止所做的:

    我的/etc/nginx/conf.d/default_site.conf:

    #!/bin/sh
    
    if [ $# -ne 1 ]; then
        echo "Wrong ammount of arguments supplied"
        echo "Usage: ./enable_website.sh website_name"
        exit 33
    fi
    
    WEBSITE_NAME="$1"
    echo "Linking apps-available/${WEBSITE_NAME}.ini to apps-enabled/${WEBSITE_NAME}.ini"
    sudo ln -s /etc/uwsgi/apps-available/${WEBSITE_NAME}.ini /etc/uwsgi/apps-enabled/${WEBSITE_NAME}.ini
    
    echo "Linking sites-available/${WEBSITE_NAME} to sites-enabled/${WEBSITE_NAME}"
    sudo ln -s /etc/nginx/sites-available/${WEBSITE_NAME} /etc/nginx/sites-enabled/${WEBSITE_NAME}
    
    
    echo "Starting website ..."
    sudo service uwsgi restart ${WEBSITE_NAME}
    echo "Started !"
    

    我有一个启用网站的bash脚本(从我的问题点2 + 3)

    uwsgi --http-socket 127.0.0.1:80 --wsgi-file app.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
    

    我还配置了一个带有Python Hello Word应用程序的uWSGI,我想将其用于第1点,但我的方法可能有误。 (nginx甚至不能在端口80上启动它......那就是请求即将到来的地方)

    经过测试:

    probably another instance of uWSGI is running on the same address (127.0.0.1:80).
    bind(): Address already in use [core/socket.c line 769]
    

    GOT:

    <?php
    if (isset($_POST['submit'])) {
    $to = "my.email@gmail.com";
    $from=$_POST['email'];
    $first_name=$_POST['first_name'];
    $last_name=$_POST['last_name'];
    $subject="Form submission";
    $message=$first_name." ".$last_name." wrote the       following:"."\n\n".$_POST['message'];
    $headers="From:".$from;
    mail($to,$subject,$message,$headers);
    echo "Mail Sent. Thank you ".$first_name." we will contact you shortly.";
    header("Location: ../pages/home.html"); /* Redirect browser */
    exit();
    }
    ?>
    

    任何有关我的选择的帮助/建议将不胜感激!圣诞快乐!!

1 个答案:

答案 0 :(得分:1)

我认为您采取了错误的步骤来实现目标。也许解决方案是在请求特定网站收入时激活正确的uWSGI应用和nginx网站,在*-enabled目录中创建符号,但有更好的解决方案。

我提出解决方案:

  1. &#34;一个统治他们所有人&#34; nginx配置,它只会将您的请求传递给正确的uWSGI应用程序。
  2. uWSGI emperor / vassals系统,带有按需附庸产卵。
  3. 在该配置中,所有项目必须具有相同方式处理的静态和媒体文件(或者足够相似,因此我们可以对所有项目使用相同的nginx配置)。或者您可以配置uWSGI来处理它们。

    1。 nginx配置:

    server {
        server_name   ~^(www\.)?(?<domain>.+)$;
        # server_name can contain any regular expression. Just remember that it should start with `~` and contain `^` and `$`
        # as you can see, we can use named capture group as an variable later
        # you can add any named capture group for later use
    
        root   /sites/$domain/public;
    
        location @default {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            include /etc/nginx/uwsgi_params;
    
            uwsgi_pass unix:/run/uwsgi/${domain}.sock;
            break;
        }
    
        location /static/ {
            try_files $uri @default;
        }
    
        location /media/ {
            try_files $uri @default;
        }
    
        location ~* ^/(android-(?:chrome|icon)[-0-9x]*\.png|ms(?:tile|-icon)[-0-9x]*\.png|browserconfig.xml|apple-(?:touch-)?icon[-0-9x]*\.png|favicon[-0-9x]*.png|favicon\.ico|manifest.json|apple-touch-icon-precomposed\.png)$ {
            try_files $uri /static/favicon$uri @default;
        }   
    
        error_page 500 502 503 504 /500.html;
        location = /500.html {
            try_files /500.html /error.html /error500.html;
        }
    
        location / {
            try_files /maintenance.html @default;
        }
    }
    

    此配置只会将来自任何域的请求传递到/run/uwsgi/domain_name_without_www.sock中的套接字。如果它不存在,nginx将抛出一些标准错误。它还将尝试直接从/sites/domain_name_without_www/public/目录提供静态和媒体文件。如果失败,将通过uWSGI提供服务。

    2。 uWSGI皇帝配置

    uWSGI拥有自己的多应用程序部署系统,称为Emperor。它还可以在访问特定套接字时启动vassal(uWSGI实例)。 emperor的示例配置可能如下所示:

    uwsgi --emperor /etc/uwsgi/apps-enabled --emperor-on-demand-directory /run/uwsgi --emperor-on-demand-extension .sock
    

    这将从/etc/uwsgi/apps-enabled加载配置,为每个配置un /run/uwsgi创建套接字,并且仅当nginx(或其他)尝试访问该应用的套接字时才启动特定应用的uWSGI服务器。所以它的行为几乎就像你在启用应用程序时创建的符号链接一样。

    在接下来的步骤中,您可以将每个vassal配置为在一段时间内没有请求时自行关闭。你也可以在附庸中创建一些预启动和预停止钩子,这样他们就可以在app的vassal启动时为nginx创建符号链接,并在vassal关闭时删除它们(每次重新加载nginx),这样你的nginx就会将请求路由到那个应用程序更快(使用虚拟主机路由它会更有效)。