我有一个静态/单页应用程序(nuxt / vue.js),该应用程序使用Laravel提供的graphql后端。
我正在尝试在单个Nginx服务器设置下为它们提供服务,其中所有路由(/ graphql除外)均作为静态文件(SPA)提供。
来自/etc/nginx/conf.d/00-default.conf
的我有
listen 80;
root /var/www/dist;
# serve singe page application
location / {
try_files $uri $uri/ /index.html;
}
# serve /graphql endpoint from laravel
location ^~ /graphql {
root /var/www/laravel/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
如何设置root
/ alias
和try_files
将此查询路由到我的laravelapp/graphql
?
现在我使用反向代理将请求发送到第二个Nginx端点,像这样
location ^~ /graphql {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:8000; # /etc/nginx/conf.d/01-laravel.conf
}
但是我不确定这是最好的方法。
答案 0 :(得分:0)
您需要为每个实例创建一个虚拟主机,并将每个实例配置为使用不同的端口
server {
listen 8081;
server_name www.domain.com ;
location ^~ /graphql {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 1m;
proxy_connect_timeout 1m;
proxy_pass http://127.0.0.1:8000; # /etc/nginx/conf.d/01-laravel.conf
}
}
server {
listen 8000;
server_name www.domain.com ;
location ^~ /graphql {
root /var/www/laravel/public;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
答案 1 :(得分:0)
您可以做的是创建一个具有2个目标组的应用程序负载平衡器,每个目标组将指向具有不同端口的nginx。并且每个应用程序将安装在不同的nginx端口下。
然后根据URL创建ELB规则,graphql URI将流量转发到laravel点,默认流量将指向另一个目标组。
这是一种整洁又不错的方法,不需要您创建任何代理:)
答案 2 :(得分:0)
尝试类似
服务器{
listen 80;
server_name example.com www.example.com;
location / {
root /path/to/first-project/public;
index index.php index.html index.htm;
try_files $uri $uri/ $uri/index.php /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
# ------- second
location /secondproject {
root /path/to/second-project/public;
index index.php index.html index.htm;
try_files $uri $uri/ $uri/index.php /index.php;
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
}
}
}