首页 > 系统相关 >Go-web应用部署的方式——(3)Nginx

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

时间:2023-12-10 14:11:26浏览次数:37  
标签:web log 配置文件 Nginx nginx proxy conf Go

摘要:本系列文章记录了几种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,
}))

标签:web,log,配置文件,Nginx,nginx,proxy,conf,Go
From: https://www.cnblogs.com/shui00cc/p/17892590.html

相关文章

  • Go-web应用部署的方式——(2)nohup、supervisor后台运行
    摘要:本系列文章记录了几种Go-web应用的部署方式,记录并解释所有相关的命令。参考:部署Go语言项目的N种方法|李文周的博客(liwenzhou.com)1.nohupnohup用于在系统后台不挂断地运行命令,不挂断指的是退出执行命令的终端也不会影响程序的运行。主流的Linux发行版中都会默认......
  • Go-web应用部署的方式——(1)Docker
    摘要:本系列文章记录了几种Go-web应用的部署方式,记录并解释所有相关的命令。参考:如何使用Docker部署GoWeb应用|李文周的博客(liwenzhou.com)1.编写Dockerfile文件#使用基础镜像FROMgolang:alpine#设置环境变量ENVGO111MODULE=on\GOPROXY=https://goproxy.cn,......
  • nginx upstream配置文件
    1.upstream使用upstream指定服务器组进行负载均衡userroot;worker_processes20;error_loglogs/error;pidlogs/nginx.pid;events{worker_connections1024;}http{log_formatmain'$remote_addr-$remote_user[$time_local]"$request"......
  • Python神器!WEB自动化测试集成工具 DrissionPage
    Python神器!WEB自动化测试集成工具DrissionPage 一、前言用requests做数据采集面对要登录的网站时,要分析数据包、JS源码,构造复杂的请求,往往还要应付验证码、JS混淆、签名参数等反爬手段,门槛较高。若数据是由JS计算生成的,还须重现计算过程,体验不好,开发效率不高。使用浏览器,......
  • 安装与使用nginx
    编译安装nginx一、从官网下载nginx软件包(https://nginx.org/en/download.html) 将从官网下载的安装包添加到opt目录下 二、安装依赖包为编译安装做准备 三、将nginx安装包解压,并移动到nginx文件夹中 四、为nginx新建一个用户,便于管理 #设置该用户不可以登录并不建立......
  • python语言在web上的应用:如何节省服务器资源?
    背景介绍在web开发中的应用广泛在web开发中的应用广泛。随着互联网的发展,web应用越来越普遍,而Python作为一种简洁、高效的编程语言,被广泛应用于web开发领域。Python提供了丰富的库和框架,如Django、Flask等,使开发人员能够快速搭建功能强大的web应用。同时,Python还具有良好的可扩展性......
  • 【SpringBootWeb入门-8】分层解耦-三层架构
    1、架构前言在讲解三层架构之前,我们先来看一段Controller代码段,代码如下:packagecom.hiker.controller;importcom.hiker.pojo.Emp;importcom.hiker.pojo.Result;importcom.hiker.utils.XmlParserUtils;importorg.springframework.web.bind.annotation.RequestMapping......
  • nginx代理knife4j接口文档
    nginx配置(11215是服务的端口信息)location~*^(/v2|/swagger-resources|/swagger-ui|/swagger-ui/index.html|/webjars/|/favicon.ico/|/doc.html){ proxy_redirectoff; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_fo......
  • Vue学习之Vue结合 ElementUI 组件库搭建Web工程项目
    创建Vue项目打开cmd命令行界面,创建一个全新的vue项目,我们命名为hello-vue,这里附上ElementUI网址如下:ElementUI#使用webpack打包工具初始化一个名为hello-vue的工程vueinitwebpackhello-vueNPM安装相关组件依赖时可能会遇到权限问题,此时使用PowerShell管理员模式运行即......
  • Privacy Policy Website(URL)
    Thissoftwarerespectsandprotectsthepersonalprivacyofallusersusingtheservice.Inordertoprovideyouwithmoreaccurateandpersonalizedservices,thissoftwarewilluseanddiscloseyourpersonalinformationinaccordancewiththeprovisions......