前端nuxt,后端php,部署问题

有个项目,前端是nuxt做的,需要用到ssr,
后端是php用的laravel框架,提供接口,
想请教下如何部署到服务器,
比如nuxt 监听端口是3000,
php 这边是80,
nginx 配置文件要怎么写? 转发操作是由node转发还是nginx转发

阅读 2.9k
2 个回答

由nginx做反向代理,由域名转发给3000端口

我大概会这样部署

nginx 部署一个 www.example.com

/api 开头的所有请求,转发到 php。
剩下的交给 nuxt。

一个示例

server {
    listen              443 ssl http2;
    listen              [::]:443 ssl http2;
    server_name www.example.com;
    root /var/www/www.example/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    #ssl
    ssl_certificate     /etc/nginx/cert/www.example.com.pem;
    ssl_certificate_key /etc/nginx/cert/www.example.com.key;

    index index.php;

    charset utf-8;
    location /api {
        try_files /not_exists @php_service;
    }
    location / {
        try_files /not_exists @nuxt;
    }


    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    #error_page 404 /index.php; #配置404页面
    
    location @nuxt {
        proxy_pass http://localhost:8003;

        add_header Content-Type text/html;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        client_max_body_size     0;
    }

    location @phpservice {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_param SCRIPT_FILENAME$realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

没法经过严格测试,部分地方可能需要调整。

这里有一个在线生成 nginx 配置的工具,https://www.digitalocean.com/...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题