0

I'm trying to deploy a Laravel 5.4 application through Nginx. So, I'm more or less following this tutorial.

My server is an Amazon EC2 running Ubuntu 16.04 with PHP 7.0. Nginx version is the 1.10.3.

At the current moment, I do not have a domain to my application, so I can only access it through the ip. I want to access my application through an URL similar to the following:

http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site

However, trying to do it shows an error 403.

If I try to access directly the public folder (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site/public), I'm receiving the home page of my application, but the links are broken. Since I got a Laravel error when I did a mistake, it seems to be working.

The port 80 is open (by this answer), and if I simply create a folder inside /var/www/html and put an index.php file I can access it through the browser. Trying to access http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com shows the Welcome to nginx default page, so nginx is working.

Artisan (php artisan serve) it seems to work in the terminal, but when I try to access it through the browser (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8000), connection is refused (and I opened the port 8000 too).

Trying to access directly through the ip () results in the same behavior for every URL (xxx.xxx.xxx.xxx shows nginx welcome, xxx.xxx.xxx.xxx/my-site returns a 403 error etc.)

I think my problem is with the sites-available files. I'm not sure how to properly name the file for my specific application, and I feel this is the problem - so nginx is not able to identify the file and so apply the configurations in the site:

Without the comments, here is my /etc/nginx/sites-available/default file:

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

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name xxx.xxx.xxx.xxx;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

And this is the content of the /etc/nginx/sites-available/my-site file:

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

    root /var/www/html/my-site/public;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm index.nginx-debian.html;

    server_name xxx.xxx.xxx.xxx/my-site;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

And here is my nginx.conf file content:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

P.s.: I already created the symbolic link for the sites-enabled folder.

I tried to use the EC2 domain (ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com in the server_name of both files, but it returned an error when I tried to restart nginx.

Removing the /my-site part of the my-site file server_name still returned a 403 error in the ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site URL. However, if I remove the default nginx welcome file (index.nginx-debian.html), I am able to access the intended home page by the http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com URL, without the my-site in it. Links are working as well; however, JS files both external and inside the same ip are not being loaded due to "Content Security Policy", and the URL is not the one I intended.

So, is there something wrong about these configurations, specifically about the server names and the name of the file?

Brian Hellekin
  • 464
  • 5
  • 21
  • [`server_name`](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name) could not contain '/'. – Paul May 23 '18 at 02:10
  • I would say that you might set the `server_name` to your `EC2` public domain. Then check if you could access or not. – Paul May 23 '18 at 02:12
  • @paul, when I used `ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com` in server name, nginx returned an error. Removed `/my-site` from the my-site file and it's still returning the same errors; I will edit the question to include these tries – Brian Hellekin May 23 '18 at 02:18
  • Could you post the contents of `nginx.conf`? – Paul May 23 '18 at 02:26
  • @paul, sure added it – Brian Hellekin May 23 '18 at 02:30
  • Well, I tried more changes and I was able to at least get a different error... I'll update the question to show that – Brian Hellekin May 23 '18 at 02:36
  • You could access `http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site/public`. `my-site/public` is called `routing`, and laravel is responsible to deal with it. – Paul May 23 '18 at 02:52
  • @paul, ok, I'll try to work on it later, since I'm able to at least get the site through `http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com`. Now I'm trying to fix the content security police, which is a separated problem that this one. Thanks! – Brian Hellekin May 23 '18 at 03:01

0 Answers0