在Windows服务器上如何通过URL无端口访问Golang网站?

在Windows服务器上如何通过URL无端口访问Golang网站? 以下是我的 main.go 文件

package main

import (
"html/template"
"log"
"net/http"
"database/sql"
"fmt"

)
const (
host     = "localhost"
port     = 5433
user     = "postgres"
password = "9docfocus9"
dbname   = "DFI"
)
var tpl *template.Template

type pageData struct {
Title     string
FirstName string
}

func init() {
tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
http.HandleFunc("/", idx)
http.HandleFunc("/products", prd)
http.HandleFunc("/contacts", cntct)
http.HandleFunc("/company", cmp)
http.HandleFunc("/solutions", sol)
http.HandleFunc("/benefits", bft)
http.HandleFunc("/careers", car)
http.HandleFunc("/document-management-overview", doc)
http.HandleFunc("/downloads", dwl)
http.HandleFunc("/downloads2", dwl2)
http.HandleFunc("/enterprise", ent)
http.HandleFunc("/features", fet)
http.HandleFunc("/workgroup", wg)
http.HandleFunc("/admin", adm)
http.HandleFunc("/admin-function", admfun)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
http.ListenAndServe(":8086", nil)
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()

err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
func idx(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Index Page",
}

err := tpl.ExecuteTemplate(w, "index.gohtml", pd)

if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}
}
func captchae(w http.ResponseWriter, req *http.Request) {

//pd := captcha.New()
 //Must set a font, Other settings have default values
//pd.SetFont("comic.ttf")
//img, str := pd.Create(4, captcha.NUM)

//err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
//if err != nil {
	//log.Println(err)
	//http.Error(w, "Internal server error", http.StatusInternalServerError)
//	return
//}
}
func prd(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Product Page",
}

err := tpl.ExecuteTemplate(w, "products.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func cntct(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Contact Page",
}

err := tpl.ExecuteTemplate(w, "contacts.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func cmp(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Company Page",
}

err := tpl.ExecuteTemplate(w, "company.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func sol(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Solution Page",
}

err := tpl.ExecuteTemplate(w, "solutions.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func bft(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Benefits Page",
}

err := tpl.ExecuteTemplate(w, "benefits.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func car(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Careers Page",
}

err := tpl.ExecuteTemplate(w, "careers.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return
}
}
func doc(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Custom Document Management Page",
}

err := tpl.ExecuteTemplate(w, "document-management-overview.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func dwl(w http.ResponseWriter, req *http.Request) {

// var (
// 	title string
// 	pdf string
// )
// rows, err := db.Query("select title, pdf from document")
// if err != nil {
// 	log.Fatal(err)
// }
// defer rows.Close()
// for rows.Next() {
// 	err := rows.Scan(&id, &name)
// 	if err != nil {
// 		log.Fatal(err)
// 	}
// 	log.Println(id, name)
// }
// err = rows.Err()
// if err != nil {
// 	log.Fatal(err)
// }



pd := pageData{
	Title: "Downloads Page",
}

err := tpl.ExecuteTemplate(w, "downloads.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func dwl2(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Downloads2 Page",
}

err := tpl.ExecuteTemplate(w, "downloads2.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func ent(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Enterprise Page",
}

err := tpl.ExecuteTemplate(w, "enterprise.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func fet(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Features Page",
}

err := tpl.ExecuteTemplate(w, "features.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func wg(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Workgroup Page",
}

err := tpl.ExecuteTemplate(w, "workgroup.gohtml", pd)
if err != nil {
	log.Println(err)
	http.Error(w, "Internal server error", http.StatusInternalServerError)
	return

}
}
func adm(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Admin Page",
}

err := tpl.ExecuteTemplate(w, "admin.gohtml", pd)
if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}
}
func admfun(w http.ResponseWriter, req *http.Request) {

pd := pageData{
	Title: "Admin Page",
}

err := tpl.ExecuteTemplate(w, "admin-function.gohtml", pd)
if err != nil {
	log.Println("LOGGED", err)
	http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
	return
}
}

更多关于在Windows服务器上如何通过URL无端口访问Golang网站?的实战教程也可以访问 https://www.itying.com/category-94-b0.html

16 回复

我没有看到任何错误

更多关于在Windows服务器上如何通过URL无端口访问Golang网站?的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


80端口已开放,我已检查防火墙设置。

当我切换到80端口时它不工作

我尝试在80端口上运行,但没有成功。

你是否修改了我指出的那行代码,让它打印返回值了?

启动时是否看到任何错误?你能从本地主机连接到你的应用程序吗?

检查防火墙中关于80端口TCP的入站规则。如有需要请创建相应规则。

抱歉。

当我尝试使用端口8086连接时工作正常,但当我尝试使用端口80时却无法工作。

无法连接我的应用程序。

请回答我上一篇文章中的所有问题。

"无法工作"只回答了一个,而我不得不猜测是哪一个问题……

Saddam_Ansari:

如何在Windows服务器上通过不带端口的URL运行Go网站?

你这是什么意思?

在80端口启动。这是HTTP的默认端口。

在Linux系统上,你需要管理员权限才能绑定该端口,我不确定在Windows上是如何操作的。

80是一个特殊端口,您可能需要以管理员身份运行应用程序,并确保没有其他应用程序正在使用该端口。当您设置端口80时,是否看到任何错误?

NobbZ:

http.ListenAndServe(":8086", nil)

当然要像这样把端口改为80:

log.Fatalln(http.ListenAndServe(":80", nil))

我最后一次问你:

当你启动服务器时,看到任何错误信息了吗?

PS:要正确回答我的问题,你可能需要检查这行代码返回的实际 error 值:

http.ListenAndServe(":8086", nil)

感谢您的回复。

这是网址:http://docfocus5.ca:8086/

我想在不显示端口的情况下运行这个,但我无法做到。

我已经尝试了很多次。

我正在使用 Windows Server 2012。已经打开了 8086 端口。

请帮助解决。

要在Windows服务器上通过URL无端口访问Golang网站,需要使用HTTP标准端口80或HTTPS标准端口443。以下是修改后的代码示例:

package main

import (
    "html/template"
    "log"
    "net/http"
    "database/sql"
    "fmt"
)

const (
    host     = "localhost"
    port     = 5433
    user     = "postgres"
    password = "9docfocus9"
    dbname   = "DFI"
)

var tpl *template.Template

type pageData struct {
    Title     string
    FirstName string
}

func init() {
    tpl = template.Must(template.ParseGlob("templates/*.gohtml"))
}

func main() {
    http.HandleFunc("/", idx)
    http.HandleFunc("/products", prd)
    http.HandleFunc("/contacts", cntct)
    http.HandleFunc("/company", cmp)
    http.HandleFunc("/solutions", sol)
    http.HandleFunc("/benefits", bft)
    http.HandleFunc("/careers", car)
    http.HandleFunc("/document-management-overview", doc)
    http.HandleFunc("/downloads", dwl)
    http.HandleFunc("/downloads2", dwl2)
    http.HandleFunc("/enterprise", ent)
    http.HandleFunc("/features", fet)
    http.HandleFunc("/workgroup", wg)
    http.HandleFunc("/admin", adm)
    http.HandleFunc("/admin-function", admfun)
    http.Handle("/favicon.ico", http.NotFoundHandler())
    http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(http.Dir("public"))))
    
    // 使用标准HTTP端口80
    err := http.ListenAndServe(":80", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }

    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }
    fmt.Println("Successfully connected!")
}

// 其他处理函数保持不变...
func idx(w http.ResponseWriter, req *http.Request) {
    pd := pageData{
        Title: "Index Page",
    }

    err := tpl.ExecuteTemplate(w, "index.gohtml", pd)
    if err != nil {
        log.Println("LOGGED", err)
        http.Error(w, "Internal serverrrrrr error", http.StatusInternalServerError)
        return
    }
}

// 其他路由处理函数...

关键修改:

  • http.ListenAndServe(":8086", nil) 改为 http.ListenAndServe(":80", nil)
  • 添加了错误处理:err := http.ListenAndServe(":80", nil)

注意事项:

  1. 在Windows服务器上运行监听80端口的程序需要管理员权限
  2. 确保Windows防火墙允许80端口入站连接
  3. 如果80端口已被其他服务占用,需要先停止该服务

如果需要在IIS后面运行,可以使用URL重写模块进行反向代理配置:

<!-- web.config -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:8086/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

这样用户就可以通过 http://yourserver.com 而不是 http://yourserver.com:8086 访问网站。

回到顶部