【转】Nginx 常用配置

nginx 之 proxy_pass详解

原文地址
在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

假设下面四种情况分别用 http://192.168.1.1/proxy/test.html 进行访问。

第一种:

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

代理到URL:http://127.0.0.1/test.html

第二种(相对于第一种,最后少一个 / )

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

代理到URL:http://127.0.0.1/proxy/test.html

第三种:

location /proxy/ {
    proxy_pass http://127.0.0.1/aaa/;
}

代理到URL:http://127.0.0.1/aaa/test.html

第四种(相对于第三种,最后少一个 / )

location /proxy/ {
    proxy_pass http://127.0.0.1/aaa;
}

代理到URL:http://127.0.0.1/aaatest.html

nginx部署前端SPA应用实践

原文地址

随着react,vue的普及,前后端分离之后,多采用nginx为静态服务器,并用nginx对api做反向代理,以实现前端SPA应用的部署。

nginx location 匹配规则

  • ~ 波浪线表示执行一个正则匹配,区分大小写
  • ~* 表示执行一个正则匹配,不区分大小写
  • ^~ 表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
  • = 进行普通字符精确匹配
  • @ 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files

browserHistory 模式的刷新问题

browserHistory 路由模式下,使用history api可以在前端进行页面跳转,但是刷新的话,就需要对链接进行一个修复(重定向)
我们可以使用nginx 的 try_files 来实现:

location / {
    root /code/app1/build;
    index index.html index.htm;
    try_files $uri $uri/ /index.html;
}
location ^~ /app {
    alias /code/app2/build;
    index index.html;
    try_files $uri $uri/ /app/index.html;
}
location ^~ /api/ {
  proxy_pass http://api.site;
}

webpackDevServer的重定向配置

const basename = '/app';
devServer: {
    proxy: {
        '/api': {
            target: 'http://api.site',
            changeOrigin: true,
            secure: false
        }
    },
    publicPath: basename,
    host: HOST,
    port: PORT,
    inline: true,
    historyApiFallback: {
        rewrites: [
            { from: new RegExp(`^${basename}`), to: `${basename}/index.html` },
            { from: /./, to: basename }
        ]
    },
    disableHostCheck: true,
    contentBase: path.join(__dirname, 'build')
}

多个SPA的部署与重定向

首先约定发布代码目录如下:

/publish_webapp/
|-- app1/
    |-- index.html
    |-- static
|-- app2/
    |-- index.html
    |-- static

nginx 配置:

location ~* ^\/(\w+) {
    root /publish_webapp/;
    index index.html;
    try_files $uri $uri/ $uri/index.html /$1/ /$1/index.html;
}

开启gzip

gzip  on;
gzip_types    text/plain application/javascript application/x-javascript text/javascript text/xml text/css;

配合webpack在打包的时候压缩静态文件,使用webpack插件compression-webpack-plugin
由于在部署至nginx服务器之前使用了webpack生成了gizp压缩之后的文件,所以就不用使用nginx来压缩静态js了,nginx只需要配置,直接使用gzip之后的文件即可。

配置gzip_static

gzip_static on;

The ngx_http_gzip_static_module module allows sending precompressed files with the “.gz” filename extension instead of regular files.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值