nginx部署web项目+接口转发

213 阅读1分钟

部署流程

  • 前端项目开发完,打包,打包文件(TeleSearch)

  • 将 TeleSearch 文件夹放在nginx/html下

image.png

  • 写好nginx配置文件

#user  nobody;
worker_processes 1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;
events {
    worker_connections 1024;
}


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

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    sendfile on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout 65;

    #gzip  on;
    upstream zytest {
        server 172.16.9.39:61000 weight=10;
    }

    server {
        listen 8888;
        server_name localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        location / {
            root html;
            index index.html index.htm;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Headers' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, PUT, POST, DELETE, OPTIONS';
            # add_header 'Access-Control-Expose-Headers' '*';
        }
        location ^~ /api/ {
            if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin *;
                add_header Access-Control-Allow-Credentials true;
                add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Progress-ID';
                return 200;
            }
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
            add_header 'Access-Control-Expose-Headers' 'X-Progress-ID';


            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;

            proxy_pass http://zytest;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

  • 这是部署在我电脑上的服务,监听8888端口

  • 浏览器地址栏访问 localhost:8888 相当于访问nginx/html/index.html

  • 访问项目地址为:localhost:8888/TeleSearch

  • 然后做了一个接口转发,拦截所有 /api/的接口请求,转发到172.16.9.39:61000,这是一个测试环境

  • 访问一下项目地址,看看页面是否正常,数据是否正常

  • 一切正常,收工,下班

vue.config.js

  • 部署看 publicPath和outputDir字段
const { defineConfig } = require('@vue/cli-service')
const path = require('path')
function resolve(dir) {
    return path.join(__dirname, dir)
}

const name = 'TeleSearch 业务中台' // 标题
const port = process.env.port || 8080 // 端口
const proxyUrl = 'http://172.16.9.39:61000'

module.exports = defineConfig({
    transpileDependencies: true,
    publicPath: '/TeleSearch',
    outputDir: 'TeleSearch',
    assetsDir: 'static',
    lintOnSave: false, //作用:是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码。
    productionSourceMap: false,
    runtimeCompiler: true, //是否使用包含运行时编译器的 Vue 构建版本。设置为 true 后你就可以在 Vue 组件中使用 template 选项了

    devServer: {
        host: '0.0.0.0',
        port: port,
        open: false,
        proxy: {
            '/api': {
                target: proxyUrl, //测试环境
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '/api',
                },
            },
        },
    },
    configureWebpack: {
        name: name,
        resolve: {
            alias: {
                '@': resolve('src'),
            },
        },
        externals: {},
    },
    chainWebpack(config) {
        config.plugin('html').tap((args) => {
            args[0].title = name
            return args
        })

        // set svg-sprite-loader
        config.module.rule('svg').exclude.add(resolve('src/assets/icons')).end()
        config.module
            .rule('icons')
            .test(/\.svg$/)
            .include.add(resolve('src/assets/icons'))
            .end()
            .use('svg-sprite-loader')
            .loader('svg-sprite-loader')
            .options({
                symbolId: 'icon-[name]',
            })
            .end()
    },
})