How i can use nginx with adonis?

I’m try to make a deploy of one api adonis and i’m trying to use nginx to enable external access to my http requests.

I install nginx and go in ssh i go to:

cd /etc/nginx
vi nginx.conf

So, i put this code:

server {
  listen 80 default_server;
  listen [::]:80 default_server;

  server_name knowhowexpressapp.com;

  # Our Node.js application
  location / {
    proxy_pass http://localhost:3003;
    proxy_http_version 1.1;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

In server name i put the domain;
In port i put 3003 because is the port that i put in .env;

I try to start nginx:

cd /usr/bin/etc/nginx
nginx

but i’m receiving:

nginx: [emerg] “server” directive is not allowed here in
/etc/nginx/nginx.conf:1

I’m forgetting something?

My nginx don’t have the sites-available folder/default archive, so i think is here that i need to put this config?

My server is centos 7

hi,

i recommend read the article write by @romain.lanz

https://dev.to/romainlanz/deploy-your-adonis-website-17ec

maybe aswer your question.

1 Like

yes, i read this article, as you can see, my config is exactly what he put and i have the error that i put in this question.

Nginx root config requires quite a lot of more. Only server is not sufficient in there. It has to go to separate file and be included in the right place or written in right place in main config file.

That’s something /etc/nginx/nginx.conf should look like (might differ based on your Nginx version and server default conf


#user  nginx;
worker_processes  1;

# load_module lib64/nginx/modules/ngx_http_fancyindex_module.so;
# load_module lib64/nginx/modules/ngx_http_geoip_module.so;
# load_module lib64/nginx/modules/ngx_http_headers_more_filter_module.so;
# load_module lib64/nginx/modules/ngx_http_image_filter_module.so;
# load_module lib64/nginx/modules/ngx_http_perl_module.so;
# load_module lib64/nginx/modules/ngx_http_xslt_filter_module.so;
# load_module lib64/nginx/modules/ngx_mail_module.so;
# load_module lib64/nginx/modules/ngx_rtmp_module.so;
# load_module lib64/nginx/modules/ngx_stream_geoip_module.so;
# load_module lib64/nginx/modules/ngx_stream_module.so;

#error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

#pid        /run/nginx.pid;


events {
    worker_connections  1024;
    use epoll;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    # Gzip, cache etc configs
    include conf.d/*.conf;
    charset UTF-8;

    # Nginx version header
    server_tokens off;

    # Redirect from www to non-www
    server {
        server_name ~^(www\.)(?<domain>.+)$;
        return 301 $scheme://$domain$request_uri;
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /srv/www/htdocs/;
            index  index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /srv/www/htdocs/;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           /srv/www/htdocs/;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   /srv/www/htdocs/;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    Allow TLS version 1.2 only, which is a recommended default these days
    #    by international information security standards.
    #    ssl_protocols        TLSv1.2;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   /srv/www/htdocs/;
    #        index  index.html index.htm;
    #    }
    #}

    include vhosts.d/*.conf;

}

Those server blocks that are commented out is where you have to add your config.
Or better is to use separate folder for it. In the end there is include vhosts.d/*.conf;
Make folder /etc/nginx/vhosts.d/ and create some config file in there. knowhowexpressapp.com.conf for example. Into that file add your server block

2 Likes

Hi, now i’m serving the nginx, but i still can’t have access from my api.

I put this code:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

    server {
        listen 80;

        server_name knowhowexpressapp.com ;

        location / {
            proxy_pass http://localhost:3333;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_cache_bypass $http_upgrade;
        }
    }


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

In server name I put the domain;
In port I put 3333 because is the port that I put in .env;

I start nginx:

cd /usr/bin/etc/nginx
nginx

Check if nginx is running:

[root@knowhowexpressapp etc]# ps aux | grep nginx
root      14143  0.0  0.0  55320  1028 ?        Ss   11:41   0:00 nginx: master process nginx
nginx     14144  0.0  0.0  55708  1936 ?        S    11:41   0:00 nginx: worker process
root      14188  0.0  0.0 112712   964 pts/2    S+   11:42   0:00 grep --color=auto nginx

My .env archive:

HOST=ip from my server
PORT=3333
NODE_ENV=production
APP_NAME=AdonisJs
APP_URL=http://${HOST}:${PORT}
CACHE_VIEWS=false
APP_KEY=GPhustNKtbIlrxawTZa6xQTIkHcjBXFr
DB_CONNECTION=pg
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=xxxx
DB_DATABASE=xxx
HASH_DRIVER=bcrypt

So, i check pm2 and my server is running:

⇆ PM2+ activated 
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ server    │ default     │ 4.1.0   │ fork    │ 12586    │ 16m    │ 34   │ online    │ 0%       │ 41.0mb   │ root     │ disabled │

also changed trustproxy to true in config/app.js

But when i try to access my api i’m getting:

Could not get any response There was an error connecting to
https://knowhowexpressapp.com/login.

My api is running only if i put the ip from my site, from the domain is getting error

Set host the same as you set reverse proxy in Nginx (localhost)

Could also try to set HOST to 0.0.0.0 in .env (0.0.0.0 means all traffic)

2 Likes

I try to set the .env host to 0.0.0.0 and localhost, i restarted the nginx, pm2 but also getting "Could not get any response