【问题标题】:Vagrant, Centos7, Nginx, Uwsgi, Django. SSH + Nginx Connection Reset then Connection Refused流浪者,Centos7,Nginx,Uwsgi,Django。 SSH + Nginx 连接重置然后连接被拒绝
【发布时间】:2020-04-29 02:58:54
【问题描述】:

解决方案

在使用静态 IP 时,请务必保留您的 IP

版本

VirtualBox Version: 6.0.0 ( I think )    
Vagrant Version: 2.2.3    
CentosBox: "centos/7"    
Nginx Version: 1.16.1   
uWSGI Version: 2.0.18   
Django Version: 2.2.1   

背景

我有两个 vagrant box 正在运行,一个测试和一个生产。唯一的区别是 IP 和内核数。我已经设置了两者,所以我可以直接 ssh 进入盒子,而不必 ssh 进入主机然后运行 ​​'vagrant ssh'

一般问题

生产版本将随机引导我退出 ssh(通过 IP 端口 22 重置连接),然后我会收到拒绝连接。如果我 ssh 进入主机然后“vagrant ssh”我仍然可以进入并且一切似乎都很好,我什至仍然可以 ping 网络上的其他计算机。但我无法从主机外部访问它,这也适用于 chrome 上的 nginx 服务器(IP 拒绝连接。)

此问题偶尔会在几分钟内自行解决,但大多数情况下需要“vagrant destroy”和“vagrant up --provision”/重新创建盒子。我也偶尔会从主机和测试盒中被启动,但是在我通过 VPN 工作之后我仍然可以从外部访问(甚至是测试中的 nginx 服务器),而且我偶尔也会被启动。好吧,但是当我注意到时我可以重新连接

VagrantFile

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Please don't change it unless you know what you're doing.
Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.hostname = "DjangoProduction"
  # Disable automatic box update checking. If you disable this, then
  # boxes will only be checked for updates when the user runs
  # `vagrant box outdated`. This is not recommended.
  # config.vm.box_check_update = false

  # Create a public network, which generally matched to bridged network.
  # Bridged networks make the machine appear as another physical device on
  # your network.
  config.vm.network "public_network", ip: "IP"

  # Share an additional folder to the guest VM. The first argument is
  # the path on the host to the actual folder. The second argument is
  # the path on the guest to mount the folder. And the optional third
  # argument is a set of non-required options.
  config.vm.synced_folder "./", "D:/abcd", type: "sshfs", group:'vagrant', owner:'vagrant'

  # Provider-specific configuration so you can fine-tune various
  # backing providers for Vagrant. These expose provider-specific options.
  # Example for VirtualBox:
  #
  config.vm.provider "virtualbox" do |v|
    v.name = "DjangoProduction"

    # test has these two commented out
    v.memory = 6000
    v.cpus = 4
  end
  #
  # View the documentation for the provider you are using for more
  # information on available options.

  ## Keys
  ### For SSH directly into the Box
  # Work Laptop Key
  config.vm.provision "file", source: ".provision/keys/work.pub", destination: "~/.ssh/work.pub"
  config.vm.provision "shell", inline: "cat ~vagrant/.ssh/work.pub >> ~vagrant/.ssh/authorized_keys"
  # Personal Laptop Key
  config.vm.provision "file", source: ".provision/keys/msi.pub", destination: "~/.ssh/msi.pub"
  config.vm.provision "shell", inline: "cat ~vagrant/.ssh/msi.pub >> ~vagrant/.ssh/authorized_keys"
  ##

  required_plugins = %w( vagrant-sshfs )
  required_plugins.each do |plugin|
      exec "vagrant plugin install #{plugin};vagrant #{ARGV.join(" ")}" unless Vagrant.has_plugin? plugin || ARGV[0] == 'plugin'
  end

  # Enable provisioning with a shell script. Additional provisioners such as
  # Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
  # documentation for more information about their specific syntax and use.
  config.vm.provision :shell, path: ".provision/boot.sh"
end

boot.sh

# networking
sudo yum -y install net-tools

ifconfig eth1 IP netmask 255.255.252.0
route add -net 10.1.0.0 netmask 255.255.252.0 dev eth1
route add default gw 10.1.0.1

# I manually set the gateway so It can be accessed through VPN

## install, reqs + drop things to places - gonna leave all that out

错误信息

姜戈

这个问题在本周早些时候开始出现,django 向我发送错误电子邮件说。它总是随机的 URL,没有一致性

OperationalError at /
(2003, 'Can\'t connect to MySQL server on \'external-ip\' (110 "Connection timed out")')

我曾经每隔一天收到一次这封电子邮件,但没有注意,但目前它每天至少向我发送 20 封邮件,而且该网站几乎无法使用 - 它要么真的很慢,要么我收到 chrome 错误:'ERR_CONNECTION_TIMED_OUT' 或'ERR_CONNECTION_REFUSED' 或 'ERR_CONNECTION_RESET' .. 一个小时就好了,然后一切都击中风扇
我最初认为这是 db 或 uwsgi 或 django 的问题,但昨天使用它时我意识到与超时和被踢出 ssh 存在相关性。

Nginx 服务器设置(我没有更改 nginx.conf)

upstream django {
  server unix:///vagrant/abcd.sock;
}

server{
  listen  8080;
  return 301 https://$host$request_uri;
}
server{
  charset utf-8;
  listen 443 ssl;

  ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
  ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;

  location / {
    uwsgi_pass  django;
    include /vagrant/project/uwsgi_params;
    uwsgi_read_timeout 3600;
    uwsgi_ignore_client_abort on;
  }

  location /static {
    alias /vagrant/static;
  }

  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /vagrant/templates/core;
  }
}

使用的 UWSGI 命令

uwsgi --socket abcd.sock --module project.wsgi --chmod-socket=664 --master --processes 8 --threads 4 --buffer-size=65535 --lazy

Nginx 错误日志

什么都没有。

消息文件

仅在发生时显示“(110 "Connection timed out")”转储

【问题讨论】:

    标签: django nginx ssh vagrant uwsgi


    【解决方案1】:

    您能否测试行为但注释“config.vm.synced_folder...”行?

    【讨论】:

    • 我想我已经想通了。该IP未在网络上保留,另一台设备占用了它,因此两者发生冲突。我很欣赏这个建议(:
    猜你喜欢
    • 2015-04-30
    • 2018-12-11
    • 2016-05-25
    • 2015-06-14
    • 2013-03-02
    • 2020-09-16
    • 2021-01-16
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多