Nuxt.js + Laravel на том же сервере nginx - PullRequest
0 голосов
/ 10 мая 2019

Я хотел бы развернуть проект nuxt + laravel на моем сервере DigitalOcean (Ubuntu 18.04).Я настроил доменное имя и сертификат SSL на нем.Я ищу правильные конфигурации для nginx для обслуживания клиента и API на одном сервере.

Я настроил клиента с прокси, но, к сожалению, API не доступен


# Redirect http to https
server {
    listen 80;
    listen [::]:80 ipv6only=on default_server;
    server_name MY_DOMAIN_NAME;
    return 302 https://$server_name$request_uri;
}

# SSL configs
server {
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/MY_DOMAIN_NAME.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

     # Use our own DH params
    ssl_dhparam /etc/nginx/dhparam.pem;

    ssl_session_cache   shared:SSL:40m;
    ssl_session_timeout 4h;

    ssl_session_tickets on;

    server_name MY_DOMAIN_NAME;
    root /var/www/MY_LARAVEL_APP_FOLDER/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    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;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ \.css {
    add_header  Content-Type    text/css;
    }
    location ~ \.js {
        add_header  Content-Type    application/x-javascript;
    }

    #client 
    location / {
        expires $expires;

        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_redirect              off;
        proxy_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass                          http://127.0.0.1:3000; # set the adress of the Node.js
    }


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


попробовал этот код, nginx говорит, что дублированное местоположение / (логично).Пожалуйста, помогите мне :) Как правильно настроить сервер для API и клиента с прокси или без?

1 Ответ

0 голосов
/ 10 мая 2019

В вашем nginx.conf есть проблема: вы не можете объявить два блока с одинаковым расположением, которое в вашем случае равно /.

Лучший способ сделать это - использовать два серверных блока с использованием виртуального хостинга.

  1. example.com (основной домен)
  2. api.example.com (поддомен API)

example.conf

# Redirect http to https
server {
    listen 80;
    listen [::]:80 ipv6only=on default_server;
    server_name example.com;
    return 302 https://$server_name$request_uri;
}

# SSL configs
server {
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/MY_DOMAIN_NAME.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

     # Use our own DH params
    ssl_dhparam /etc/nginx/dhparam.pem;

    ssl_session_cache   shared:SSL:40m;
    ssl_session_timeout 4h;

    ssl_session_tickets on;

    server_name example.com;
    root /var/www/MY_LARAVEL_APP_FOLDER/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    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;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ \.css {
    add_header  Content-Type    text/css;
    }
    location ~ \.js {
        add_header  Content-Type    application/x-javascript;
    }

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

и для API api.example.conf

# Redirect http to https
server {
    listen 80;
    listen [::]:80 ipv6only=on default_server;
    server_name api.example.com;
    return 302 https://$server_name$request_uri;
}

# SSL configs
server {
    listen 443 ssl http2;
    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/MY_DOMAIN_NAME.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

     # Use our own DH params
    ssl_dhparam /etc/nginx/dhparam.pem;

    ssl_session_cache   shared:SSL:40m;
    ssl_session_timeout 4h;

    ssl_session_tickets on;

    server_name api.example.com;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    charset utf-8;

    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;


    location ~ \.css {
    add_header  Content-Type    text/css;
    }
    location ~ \.js {
        add_header  Content-Type    application/x-javascript;
    }

    #client 
    location / {
        expires $expires;

        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_redirect              off;
        proxy_read_timeout          1m;
        proxy_connect_timeout       1m;
        proxy_pass                          http://127.0.0.1:3000; # set the adress of the Node.js
    }

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