Tenho trabalhado em um pequeno aplicativo web e queria testar o nginx e o Golang para meu backend. Sou bem novo no Golang, pois este é meu primeiro projeto web Golang. De qualquer forma, estou tendo um problema com proxy reverso para minha página de login HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="loginStyle.css">
<title>Cosmic Cloud Login</title>
</head>
<body>
<div class="loginContainer">
<h1 class="headerText">Login</h1>
<p>Please enter your login information</p>
<form action="/login" method="POST">
<div class="inputGroup">
<label for="Username">Username</label>
<input type="Username" id="Username" name="Username" placeholder="Enter your username" required>
</div>
<div class="inputGroup">
<label for="Password">Password</label>
<input type="Password" id="Password" name="Password" placeholder="Enter your password" required>
</div>
<button type="submit" class="loginButton">Login</button>
</form>
</div>
</body>
</html>
O arquivo nginx.conf é o seguinte:
server {
listen 80;
location / {
root /var/www/html;
index index.html;
}
location /login {
proxy_pass http://0.0.0.0:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
e o arquivo Go é o seguinte:
package main
import (
"fmt"
"net/http"
)
var validUsername = "test"
var validPassword = "test1234"
func loginHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid Request method", http.StatusMethodNotAllowed)
return
}
username := r.FormValue("Username")
password := r.FormValue("Password")
if username == validUsername && password == validPassword {
fmt.Fprint(w, "Login Successfull!")
} else {
http.Error(w, "Invalid Username or Password", http.StatusUnauthorized)
}
}
func main() {
http.HandleFunc("/login", loginHandler)
fmt.Println("Server is running on :8080")
http.ListenAndServe(":8080", nil)
}
Ao enviar uma solicitação POST, o backend do Golang nunca a recebe, a menos que você especifique a porta 8080. (por exemplo, "http://IP/login" não funciona) ("http://IP:8080/login" funciona) Eu tentei ambos na máquina local e em outra máquina na rede. TLDR: o nginx não está enviando corretamente a solicitação POST do servidor web nginx (porta 80) para a porta onde o backend do Golang está escutando (porta 8080). Qualquer ajuda é muito apreciada, TYIA!
Editar: Provavelmente não é um problema de firewall, pois desabilitei o ufw no ambiente de teste.