Go-web应用部署的方式——(3)Nginx

摘要:本系列文章记录了几种Go-web应用的部署方式,记录并解释所有相关的命令。

参考:部署Go语言项目的 N 种方法 | 李文周的博客 (liwenzhou.com)

 抛开宝塔面板一键部署的方式,这里记录下手动使用Nginx部署应用的过程,以及前后端是否分离的区别

1. 安装Nginx

sudo yum install epel-release
sudo yum install nginx

设置开机启动

sudo systemctl enable nginx

启动Nginx,查看运行状态

sudo systemctl start nginx
sudo systemctl status nginx

2. 配置文件

通过上面的方法安装的 nginx,所有相关的配置文件都在 /etc/nginx/ 目录中。Nginx 的主配置文件是 /etc/nginx/nginx.conf,默认还有一个nginx.conf.default的配置文件示例。

应用独立的 Nginx 服务配置文件都必须以 .conf 结尾,并存储在 /etc/nginx/conf.d 目录中。

如果是宝塔面板,主配置文件在 /www/server/nginx/conf/nginx.conf,面板部署的应用各自的配置文件在 /www/server/panel/vhost/nginx 中。

3. Nginx反向代理部署应用程序

修改配置文件

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        access_log   /var/log/bluebell-access.log;
        error_log    /var/log/bluebell-error.log;

        location / {
            proxy_pass                 http://127.0.0.1:8084;
            proxy_redirect             off;
            proxy_set_header           Host             $host;
            proxy_set_header           X-Real-IP        $remote_addr;
            proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
        }
    }
}

执行 nginx -t 检查配置文件语法,执行 nginx -s reload 重新加载配置文件。

4. Nginx分离静态文件请求

我们可以分离静态文件请求与后端接口请求。在配置文件的server{}中修改如下

        # 静态文件请求
        location ~ .*\.(gif|jpg|jpeg|png|js|css|eot|ttf|woff|svg|otf)$ {
            access_log off;
            expires    1d;
            root       /data/app/bluebell;
        }

        # index.html页面请求
        # 因为是单页面应用这里使用 try_files 处理一下,避免刷新页面时出现404的问题
        location / {
            root /data/app/bluebell/templates;
            index index.html;
            try_files $uri $uri/ /index.html;
        }

        # API请求
        location /api {
            proxy_pass                 http://127.0.0.1:8084;
            proxy_redirect             off;
            proxy_set_header           Host             $host;
            proxy_set_header           X-Real-IP        $remote_addr;
            proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
        } 

5. 前后端分离部署

前后端分离时,Nginx对前端服务器反向代理,前端将API服务请求转发到后端服务器,此处在后端需要使用 github.com/gin-contrib/cors 库来支持跨域请求。

// 为gin路由Use中间件
// Default默认允许所有跨域请求
r.Use(cors.Default())
// 或自定义配置
r.Use(cors.New(cors.Config{
    AllowOrigins:     []string{"https://foo.com"},
    AllowMethods:     []string{"PUT", "PATCH"},
    AllowHeaders:     []string{"Origin"},
    ExposeHeaders:    []string{"Content-Length"},
    AllowCredentials: true,
    AllowOriginFunc: func(origin string) bool {
        return origin == "https://github.com"
    },
    MaxAge: 12 * time.Hour,
}))

6. 实战举例:我将服务进程部署以前后端不分离的方式,部署在 106.55.245.213 服务器的 8084 端口,希望通过域名 gnalog.cn 访问,并强制https访问

server {
listen 80;
server_name gnalog.cn www.gnalog.cn;

location / {
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;
server_name gnalog.cn www.gnalog.cn;

ssl_certificate /www/server/panel/vhost/cert/gnalog/gnalog.cn_bundle.crt;
ssl_certificate_key /www/server/panel/vhost/cert/gnalog/gnalog.cn.key;

location / {
proxy_pass http://106.55.245.213:8084;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location ~ ^/([^/]+)/(.*) {
proxy_pass http://106.55.245.213:8084/$1/$2$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

 

posted on 2023-12-10 14:07  shui00cc  阅读(77)  评论(0编辑  收藏  举报