M
M
mrFlyer2019-01-28 02:44:19
Nginx
mrFlyer, 2019-01-28 02:44:19

How to configure Nginx in OpenServer for an SSR application?

Greetings!
There is an SSR application on NuxtJS that is currently running at localhost:3000
I want it to be available at the domain name my-domain.dev
I threw the Apache-PHP-7-x64+Nginx-1.12_vhostn.conf file into the /my-domain folder /public
And then a stupor ... the first time I'm dealing with NodeJS and Nginx =)
1. The domain is still processing Apache, which probably needs to be redirected to the /my-domain/public/api/ folder where the backend will be?
2. How to set proxy my-domain.dev -> localhost:3000 in configs ? The official NuxtJS doc does not help, no matter what I do, the server does not start at all after that. Or in the beginning it is necessary to deal with the Apache?
UPDATE
I sort of figured it out with the proxy.
In Apache-PHP-7-x64+Nginx-1.12_vhostn.conf replaced

proxy_pass                http://%ips%:%httpbackport%/;

on
proxy_pass http://localhost:3000;
The application began to open by domain. But the statics (pictures, etc.) were not displayed.
Removed the block:
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|cur|swf)$ {
 root     "%hostdir%";
 expires  7d;
}

And everything seemed to work. Probably clumsy, but I hope it will be enough for testing on a local server.
The question remains, how to pass all requests from /my-domain/public/api to the PHP backend?
It is necessary to somehow exclude the api folder from the proxy ...
UPDATE2
Added a block
location /api{
        proxy_buffer_size         64k;
        proxy_buffering           on;
        proxy_buffers             4 64k;
        proxy_connect_timeout     5s;
        proxy_ignore_client_abort off;
        proxy_intercept_errors    off;
        proxy_pass                http://%ips%:%httpbackport%;
        proxy_pass_header         Server;
        proxy_read_timeout        5m;
        proxy_redirect            off;
        proxy_send_timeout        5m;
        proxy_set_header          Host $host;
        proxy_set_header          X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header          X-Real-IP $remote_addr;
        proxy_set_header          X-Forwarded-Proto $scheme;
}

And actually, through mod_rewrite, I caught the request to /public/index.php, then I already registered the route for /api in the backend and forward with the song =)
In general, everything turned out to be easier than I thought. If anyone has a comment, I will be grateful!
Default OpenServer config:
#-----------------------------------------------#
# Конфигурация хоста для сервера Nginx
# Начало блока конфигурации хоста
#-----------------------------------------------#

server {
    listen         %ip%:%httpport%;
    listen         %ip%:%httpsport% ssl;
    server_name    %host% %aliases%;
    
    ssl_certificate               "%sprogdir%/userdata/config/cert_files/server.crt";
    ssl_certificate_key           "%sprogdir%/userdata/config/cert_files/server.key";
    
    #add_header Strict-Transport-Security "max-age=94608000";
    
    #if ($request_method !~* ^(GET|HEAD|POST)$ ){return 403;}
    location ~ /\. {deny all;}

    location / {
        proxy_buffer_size         64k;
        proxy_buffering           on;
        proxy_buffers             4 64k;
        proxy_connect_timeout     5s;
        proxy_ignore_client_abort off;
        proxy_intercept_errors    off;
        proxy_pass                http://%ips%:%httpbackport%/;
        proxy_pass_header         Server;
        proxy_read_timeout        5m;
        proxy_redirect            off;
        proxy_send_timeout        5m;
        proxy_set_header          Host $host;
        proxy_set_header          X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header          X-Real-IP $remote_addr;
        proxy_set_header          X-Forwarded-Proto $scheme;
    }

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js|cur|swf)$ {
        root     "%hostdir%";
        expires  7d;
    }

    location ^~ /apacheicons/ {
        alias    %sprogdir%/modules/http/%httpdriver%/icons/;
    }
    location ^~ /apacheerror/ {
        alias    %sprogdir%/modules/http/%httpdriver%/error/;
    }

    # Подключение веб-инструментов
    #---------------------------------------#
    # <Не изменяйте этот блок конфигурации>
    
    location /openserver/ {
        %allow%allow    all;
        allow    127.0.0.0/8;
        allow    ::1/128;
        allow    %ips%;
        deny     all;

        location /openserver/server-status {
            stub_status on;
        }

        proxy_buffer_size         64k;
        proxy_buffering           on;
        proxy_buffers             4 64k;
        proxy_connect_timeout     5s;
        proxy_ignore_client_abort off;
        proxy_intercept_errors    off;
        proxy_pass                http://%ips%:%httpbackport%/openserver/;
        proxy_pass_header         Server;
        proxy_read_timeout        5m;
        proxy_redirect            off;
        proxy_send_timeout        5m;
        proxy_set_header          Host $host;
        proxy_set_header          X-Forwarded-For $http_x_forwarded_for;
        proxy_set_header          X-Real-IP $remote_addr;
        proxy_set_header          X-Forwarded-Proto $scheme;

        location ~* ^/openserver/.+\.(jpg|jpeg|gif|png|ico|css|js|cur|swf)$ {
            root     "%sprogdir%/modules/system/html";
            expires  7d;
        }
    }
    
    # <Не изменяйте этот блок конфигурации/>
    #---------------------------------------#
}

#-----------------------------------------------#
# Конец блока конфигурации хоста
#-----------------------------------------------#

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TyzhSysAdmin, 2019-01-28
@POS_troi

In the file C:\Windows\System32\drivers\etc\hosts add the line.
127.0.0.1 my-domain.dev

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question