laravel에 대한 로컬 nginx 하위 도메인 생성

laravel에 대한 로컬 nginx 하위 도메인 생성

현재 로컬 도메인에 대해 작동 중인 역방향 프록시가 있습니다.https://domain.test항목으로 /etc/hosts

127.0.0.1 domain.test

https://domain.testhttps://domain.test/api모든 API 호출을 처리하기 위해 laravel로 이동하는 동안 스파 웹 사이트로 이동합니다 .

예를 들어 하위 도메인을 갖고 싶습니다.https://sub.domain.test처리할 또 다른 API 서버가 있습니다.https://sub.domain.test/api

이것은 현재 /etc/nginx/sites-enabled/domain.conf첫 번째 서버 블록이 https로 리디렉션되는 것입니다. https://domain.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://domain.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비어있다

따라서 하위 도메인 액세스를 허용하려면 첫 번째 블록을 수정해야 합니다.

관련 정보