我目前有一个到本地域的有效反向代理:https://域名.test进入
/etc/hosts
127.0.0.1 domain.test
https://domain.test
访问 spa 网站,同时https://domain.test/api
访问 laravel 来处理所有 api 调用。
我想要一个子域名,例如https://sub.domain.test并有另一个 api 服务器来处理https://sub.domain.test/api
这是我当前的/etc/nginx/sites-enabled/domain.conf
第一个服务器块重定向到 https。
https://域名.test由水疗中心处理https://domain.test/api通过端口 8282 反向代理。
access_log /var/log/nginx/domain-access.log;
error_log /var/log/nginx/domain-error.log;
server {
listen 80 default_server;
listen [::] default_server;
return 301 https://domain.test$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 ipv6only=on default_server;
ssl_certificate /etc/ssl/certs/nginx-selfsigned.crt;
ssl_certificate_key /etc/ssl/private/nginx-selfsigned.key;
server_name domain.test;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_ssl_server_name on;
proxy_set_header Upgrade $http_update;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8282;
}
}
server {
listen 8282;
server_name localhost;
root /home/gmhafiz/domain/public;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
error_log /var/log/nginx/nginx-proxy-laravel.error.log;
access_log /var/log/nginx/nginx-proxy-laravel.access.log;
}
我尝试添加两个新的服务器块来处理子域名https://sub.domain.test并添加了一个条目/etc/hosts
server {
server_name sub.domain.test;
location /api {
proxy_pass http://127.0.0.1:8283;
}
}
server {
listen 8383;
server_name localhost;
root /home/gmhafiz/projects/subdomain/public;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 600;
include fastcgi_params;
}
error_log /var/log/nginx/nginx-proxy-laravel-subdomain.error.log;
access_log /var/log/nginx/nginx-proxy-laravel-subdomain.access.log;
}
但它不起作用,因为我认为第一个服务器块使用 301 重定向。
我也无法添加监听 80 和 443 的重复服务器块。我仍然需要https://域名.test上班。
server {
listen 80 default_server;
listen [::] default_server;
return 301 https://queue.dribl.test$request_uri;
}
我都sudo nginx -t
做过sudo systemctl restart nginx.service
我为子域 api 制作了一个测试控制器来处理https://sub.domain/api/test curl -k --location --request GET 'https://sub.domain.test/api/test'
但它又回来了
{"error": "Endpoint not found."}
两者/var/log/nginx/domain-access.log
都有日志条目:
127.0.0.1 - - [08/Jan/2021:13:51:36 +1100] "GET /api/test HTTP/2.0" 400 31 "-" "curl/7.68.0"
/var/log/nginx/nginx-proxy-laravel.access.log
127.0.0.1 - - [08/Jan/2021:13:51:36 +1100] "GET /api/test HTTP/1.0" 400 31 "-" "curl/7.68.0"
/var/log/nginx/nginx-proxy-laravel-subdomain.error.log
并且/var/log/nginx/nginx-proxy-laravel-subdomain.access.log
为空
因此显然必须修复第一个块以允许子域访问。