簡體   English   中英

Vagrant、Centos7、Nginx、Uwsgi、Django。 SSH + Nginx 連接重置然后連接被拒絕

[英]Vagrant, Centos7, Nginx, Uwsgi, Django. SSH + Nginx Connection Reset then Connection Refused

解決方案

使用靜態 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   

背景

我有兩個流浪盒子在運行,一個測試和一個生產。 唯一的區別是 IP 和內核數。 我已經設置了兩個,所以我可以直接 ssh 進入盒子,而不必 ssh 進入主機然后運行“vagrant ssh”

一般問題

生產版本將隨機引導我退出 ssh(通過 IP 端口 22 重置連接),然后我將收到連接被拒絕。 如果我 ssh 進入主機,然后 'vagrant ssh' 我仍然可以進入並且一切似乎都很好,我什至仍然可以 ping 網絡上的其他計算機。 但是我無法從主機外部訪問它,這也適用於 nginx 服務器(IP 拒絕連接)。

該問題偶爾會在幾分鍾內自行解決,但大部分時間需要“vagrant destroy”和“vagrant up --provision”/重新創建框。 我也偶爾會從主機和測試盒中被啟動,但是我仍然可以在(甚至是測試中的 nginx 服務器)之后從外部訪問我正在通過 VPN 工作,我也偶爾會被啟動,因為好吧,但是當我注意到時我可以重新連接

流浪文件

# -*- 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

啟動文件

# 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")' 轉儲

您可以測試行為但評論“config.vm.synced_folder...”這一行嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM