laravel 用のローカル nginx サブドメインを作成する

laravel 用のローカル nginx サブドメインを作成する

現在、ローカル ドメインへのリバース プロキシが動作しています。https://ドメイン.testエントリーのある /etc/hosts

127.0.0.1 domain.test

https://domain.testSPA Web サイトにアクセスし、https://domain.test/apiすべての API 呼び出しを処理するために Laravel にアクセスします。

サブドメインが欲しいです。例: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;
}

サブドメインを処理するために2つの新しいサーバーブロックを追加してみました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 -tsudo 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"

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空です

したがって、サブドメイン アクセスを許可するには、明らかに最初のブロックを修正する必要があります。

関連情報