求教 nginx 配置

2022-08-21 09:46:11 +08:00
 kestrelBright

现在前后端分离没问题,想要后端也能暴露出来,可以通过域名+端口直接访问, 最好还是同一域名

server
    {
        listen 80;
        server_name www.xxx.cc;
        index index.html index.htm;

        set $base_path "/xxx/xxx/xx";
        
        root "${base_path}";
        

        location  / {
            alias   "${base_path}/dist/";
            index  index.html index.htm;
            try_files $uri $uri/ /index.html$is_args$args;
        }

        location /api/ {
            proxy_pass http://127.0.0.1:9222/;
            proxy_set_header Host    $host;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
    server {
        listen   9222;
        server_name 127.0.0.1 www.xxx.cc;

        location / {
            root   /xxx/xxx/xx;
            index  index.php;
            try_files $uri $uri/ $uri/index.php$is_args$args;

            location ~ ^(.+?\.php)($|/.*) {
                include        fastcgi.conf;
                fastcgi_pass   127.0.0.1:9005;
                fastcgi_index  index.php;
            }
        }
    }
10113 次点击
所在节点    程序员
8 条回复
gaogang
2022-08-21 10:12:58 +08:00
你这感觉没啥问题呀
不是通过 www.xxx.cc/apiwww.xxx.cc:9222 都能访问到后端吗
securityCoding
2022-08-21 11:32:39 +08:00
whale
2022-08-21 11:52:49 +08:00
www.xxx.cc/api 不就是后端地址了,不知道是出于什么考虑加的第二个 server ,准备放二级域名 api.xxx.cc
kestrelBright
2022-08-21 12:07:42 +08:00
@whale 之前试过这种,会出现 file not found
···
server
{
listen 80;
server_name www.xxx.cc;
index index.html index.htm;

set $base_path "/xxx/xxx/xx";

root "${base_path}";


location / {
alias "${base_path}/dist/";
index index.html index.htm;
try_files $uri $uri/ /index.html$is_args$args;
}

location /api/ {
rewrite ^/api/(.+)$ $1;
root /xxx/xxx/xx;
index index.php;
try_files $uri $uri/ $uri/index.php$is_args$args;

location ~ ^(.+?\.php)($|/.*) {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9005;
fastcgi_index index.php;
}
}
}
···
kestrelBright
2022-08-21 12:08:21 +08:00
@gaogang 访问不到,会 file not found
skys215
2022-08-21 12:41:16 +08:00
@kestrelBright 看 nginx 去访问了哪个路径就知道哪里配置不对了
kestrelBright
2022-08-21 15:17:15 +08:00
@skys215 看 access 日志 路径是对的
kestrelBright
2022-08-21 15:32:06 +08:00
卧槽。。。现在重启 nginx 后 又行了。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/874296

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX