美文网首页
nginx 服务搭建

nginx 服务搭建

作者: that_is_this | 来源:发表于2021-02-21 22:38 被阅读0次

    ubuntu 18.04 环境下搭建 nginx 服务,跑 go 程序;

    参考地址:
    如何在Ubuntu 18.04上使用Nginx部署Go Web应用程序
    https://www.howtoing.com/how-to-deploy-a-go-web-application-using-nginx-on-ubuntu-18-04

    1. 安装 nginx

    sudo apt-get install nginx

    sudo systemctl start nginx
    sudo systemctl stop nginx
    sudo systemctl restart nginx

    防火墙端口的设置

    iptables -I INPUT 5 -p tcp --dport 80 -j ACCEPT

    1. golang 安装

    add-apt-repository ppa:longsleep/golang-backports
    apt-get update
    sudo apt-get install golang-go

    鉴定是否安装成功
    go version
    配置信息查看
    go env
    $GOPATH

    1. 部署 go 程序

    /root/go/main.go

    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    func main() {
        http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
            fmt.Fprintf(w, "Hello World")
        })
    
        http.HandleFunc("/greet/", func(w http.ResponseWriter, r *http.Request) {
            name := r.URL.Path[len("/greet/"):]
            fmt.Fprintf(w, "Hello %s\n", name)
        })
    
        http.ListenAndServe(":9990", nil)
    }
    

    然后编译可执行文件:
    go build main.go

    设置系统单元文件:
    在 /lib/systemd/system/ 下创建 goweb.service 文件,并写以下内容

    [Unit]
    Description=goweb
    
    [Service]
    Type=simple
    Restart=always
    RestartSec=5s
    ExecStart=/root/go/main
    
    [Install]
    WantedBy=multi-user.target
    

    启动服务:
    sudo service goweb start
    sudo service goweb stop
    sudo service goweb status

    1. negix 设置反向代理

    进入 cd /etc/nginx/sites-available 然后创建 xxx.com 文件,然后写入

    server {
        server_name xxx.com www.xxx.com;
    
        location / {
            proxy_pass http://localhost:9990;
        }
       #文件的下载
        location /download {
            alias /root/files;
            add_header Content-Type "application/octet-stream";
        }
    }
    

    创建连接:

    sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/your_domain

    然后重新启动 nginx 服务:

    sudo nginx -s reload

    然后现在访问 http://xxx.com 就看到的服务返回的内容了;

    1. nginx 代理转发 apache2

    5.1 更改 /etc/apache2/ports.conf 的端口 为 9090
    更改 /etc/apache2/sites-available/default.conf 的端口为 9090
    5.2 重启 apa2che 服务

    5.3 更改 nginx 的配置文件
    添加

        location / {
            proxy_pass http://127.0.0.1:9090;
        }
    

    5.4 重启 并重新加载 nginx 服务

    相关文章

      网友评论

          本文标题:nginx 服务搭建

          本文链接:https://www.haomeiwen.com/subject/zedcfltx.html