V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kestrelBright
V2EX  ›  程序员

求教 nginx 配置

  •  
  •   kestrelBright · 2022-08-21 09:46:11 +08:00 · 10099 次点击
    这是一个创建于 604 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    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;
                }
            }
        }
    
    8 条回复    2022-08-21 15:32:06 +08:00
    gaogang
        1
    gaogang  
       2022-08-21 10:12:58 +08:00
    你这感觉没啥问题呀
    不是通过 www.xxx.cc/apiwww.xxx.cc:9222 都能访问到后端吗
    whale
        3
    whale  
       2022-08-21 11:52:49 +08:00
    www.xxx.cc/api 不就是后端地址了,不知道是出于什么考虑加的第二个 server ,准备放二级域名 api.xxx.cc
    kestrelBright
        4
    kestrelBright  
    OP
       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
        5
    kestrelBright  
    OP
       2022-08-21 12:08:21 +08:00
    @gaogang 访问不到,会 file not found
    skys215
        6
    skys215  
       2022-08-21 12:41:16 +08:00
    @kestrelBright 看 nginx 去访问了哪个路径就知道哪里配置不对了
    kestrelBright
        7
    kestrelBright  
    OP
       2022-08-21 15:17:15 +08:00
    @skys215 看 access 日志 路径是对的
    kestrelBright
        8
    kestrelBright  
    OP
       2022-08-21 15:32:06 +08:00
    卧槽。。。现在重启 nginx 后 又行了。。。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   957 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 21:01 · PVG 05:01 · LAX 14:01 · JFK 17:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.