【nginx】前后端代理配置

文件目录

test
│
├─test0
│      index.html
├─test1
│      index.html
└─test2
    │  index.html
    ├─test2-1
    │      index.html
    └─test2-2
            index.html

代理前端

代理单个前端时,以下eg1、eg2代理的是同一个文件,不用的是url

  • localhost:8080/
  • localhost:8080/test0
server {
    listen 8080;
    server_name  localhost;

  # eg1
    location / {
        root   /root/test/test0;
        index  index.html index.htm;
    }

  # eg2
    location /test0 {
        root   /root/test;
        index  index.html index.htm;
    }
    
  # eg3
    location /test1 {
        alias  /root/test/test1/;
        index  index.html index.htm;
    }
}

细心地读者发现还有第三个代理eg3、它的不同在于19行,是以alias开头的代理。那么他有什么不同呢,按照上面代理文件的路径,test1与test0是一样的,也就是说eg1和eg3是一样的代理。

location root/alias 文件实际路径
eg1 / /root/test/test0 /root/test/test0/index.html
eg2 /test0 /root/test /root/test/test0/index.html
eg3 /test1 /root/test/test1/ /root/test1/test1/index.html

简单的分析出:
root:root + location 为实际文件路径
alias:alias 为实际文件路径(ps:必须是以/结尾,因为代理的东西在此目录下)

代理多个前端

当代理多个静态文件时,容易发生404问题。
若把单个配置成location /时,又没有问题,那么你需要参考以下配置。
ps:一般根路径用root,其他为alias代理。

server {
    listen 8080;
    server_name  localhost;

    location / {
        root  /root/test/test0;
        index  index.html index.htm;
    }

    location /test1 {
        alias  /root/test/test1/;
        index  index.html index.htm;
    }
    
    location /test2 {
        alias  /root/test/test2/;
        index  index.html index.htm;
    }

    location /test2-1 {
        alias  /root/test/test2/test2-1/;
        index  index.html index.htm;
    }

    location /test2-2 {
        alias  /root/test/test2/test2-2/;
        index  index.html index.htm;
    }
}

代理前端和后端

有关^~请看FAQ
12行最后的/api/;分号前面的/问题请看FAQ

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html; # 解决页面刷新 404 问题
}

location ^~ /api {  # ^~/api/表示匹配前缀为api的请求
    # 跨域问题
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    proxy_pass  http://www.test.com:5000/api/;  # 注:proxy_pass的结尾有/, -> 效果:会在请求时将/api/*后面的路径直接拼接到后面
}

代理https请求&转发

因为微信公众号的回调只能调用https,有些时候可能会用到。
这里需要自己了解一下https,简而言之需要证书,针对某一个url,这里展示一个代理示例

server {
    listen      443  ssl;
    server_name  xxx.somliy.top; # https的url

    # 申请https证书会给这两个文件
    ssl on;
    ssl_certificate      /etc/nginx/test/xxx.somliy.top_bundle.crt;
    ssl_certificate_key  /etc/nginx/test/xxx.somliy.top.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_prefer_server_ciphers  on;
    ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;


    location / {
        proxy_http_version 1.1; #代理使用的http协议
        proxy_set_header Host $host; #header添加请求host信息
        proxy_set_header X-Real-IP $remote_addr; # header增加请求来源IP信息
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 增加代理记录
        proxy_pass http://x.x.x.x:8080; # 代理到的真正地址
    }
}

FAQ

前端部署后,刷新404问题

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html; # 解决页面刷新 404 问题
}

代理配置中,location的url尾部的/需不需要

简单测试了一下,若是仅仅前端代理,是没有影响的,无论是root还是alias
那区别是什么呢,当代理的内容为地址时,http://test.com/dir

url尾部的/表示目录,没有/表示文件,是否需要加,根据情况选择

代理到后端时,不认识代理标记

使用^~当做前缀,正则匹配然后代理

location ^~ /api/ {
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    proxy_pass http://localhost:82/;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355

推荐阅读更多精彩内容