2

I can't load css and js files on my MAC. I get 404 for those files. on Ubuntu in nginx.conf I just add on the end rewrite ^/assets/([a-z\-]+)-([a-z0-9]+).(css|js) /assets/$1.$3; and it works.

But I don't know where put it on osx because when I wrote it like on Ubuntu I get syntax err...

my nginx config file looks like:

worker_processes  auto;

pid        logs/nginx.pid;

events {
    worker_connections  1024;
}


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


    access_log  logs/access.log;
    error_log  logs/error.log;
    sendfile        on;
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  default;

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

        }

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

    }


        # HTTPS server
        server {
                server_name     local.beer.co.uk;
                listen  80;
                return  301     https://$host$request_uri;
        }

   server {
    listen                     443 ssl;
    server_name                local.beer.co.uk local.beer.telegraph.co.uk;

    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate            /usr/local/etc/nginx/cert.pem;
    ssl_certificate_key        /usr/local/etc/nginx/cert.key;

    gzip_disable "msie6";
    gzip_types text/plain application/xml application/x-javascript text/css application/json text/javascript;

    access_log  /usr/local/var/log/nginx/access.log;
    error_log   /usr/local/var/log/nginx/error.log debug;
    log_not_found off;
    root    /Users/RobDee/workspace/beer;

    location /.htpasswd
    {
        return 403;
    }
    location ~ \.css {
        root /Users/RobDee/workspace/beer/web;
        expires max;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|js|woff|woff2|ttf)$ {
        root /Users/RobDee/workspace/beer/web;
        access_log off;
        expires max;
    }

    location ~* \.(js|css)$ {
        expires 1y;
        log_not_found off;

    }

    location /
    {
        root /Users/RobDee/workspace/beer/web;
        try_files $uri $uri/ /app_dev.php$is_args$args;
         index app_dev.php;
    }

    location ~ \.php$ {
        root /Users/RobDee/workspace/beer/web;
        fastcgi_pass   127.0.0.1:9003;
        fastcgi_index  app_dev.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    }
    include servers/*;

    }
drookie
  • 8,051
  • 1
  • 17
  • 27
RobDee
  • 115
  • 2
  • 3
  • 6
  • You should see the the error log for a corresponding `server{}` for an explanation of 404 errors. Probably (like 99% chance) the file wasn't found, because you pointer the server at wrong docroot. – drookie Oct 28 '16 at 12:54
  • You have a bunch of different `root` directives in your `location`s. This is [a common nginx mistake](https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#root-inside-location-block). – Michael Hampton Oct 28 '16 at 14:57
  • Please post "the syntax err" that you mentioned in the question. Nginx may also show syntax error if we forget to type a semicolon on a line. – Pothi Kalimuthu Oct 28 '16 at 16:30
  • @Pothi when I put rewrite at the end server I get `[emerg] "rewrite" directive is not allowed here in /usr/local/etc/nginx/nginx.conf:155` after `nginx -t` – RobDee Oct 30 '16 at 09:56
  • I understood where things would have gone wrong after seeing the actual error message. Please check out my answer for the solution and an explanation on why you received the error. – Pothi Kalimuthu Oct 30 '16 at 11:29
  • @Pothi so now I get Fatal error: Call to a `member function getCode() on boolean in (...) Session.php on line 71`and in error.log I got `bind() to 0.0.0.0:80 failed (13: Permission denied)`. I read about it but I don't know how set permission – RobDee Oct 31 '16 at 08:42
  • Create a separate question, please. http://serverfault.com/help/someone-answers – Pothi Kalimuthu Oct 31 '16 at 09:57

1 Answers1

2

rewrite directive can be used only within the context of server, location or "if" block. For example, it can not be used in "http" block. You must have used it within the event block or the http block (alongside other server blocks). Please see where I have used the rewrite directive.

worker_processes  auto;

pid        logs/nginx.pid;

events {
worker_connections  1024;

}


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


    access_log  logs/access.log;
    error_log  logs/error.log;
    sendfile        on;
    keepalive_timeout  65;


    server {
        listen       80;
        server_name  default;

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

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


    # HTTPS server
    server {
        server_name     local.beer.co.uk;
        listen  80;
        return  301     https://$host$request_uri;
    }

    server {
        listen                     443 ssl;
        server_name                local.beer.co.uk local.beer.telegraph.co.uk;

        ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
        ssl_certificate            /usr/local/etc/nginx/cert.pem;
        ssl_certificate_key        /usr/local/etc/nginx/cert.key;

        gzip_disable "msie6";
        gzip_types text/plain application/xml application/x-javascript text/css application/json text/javascript;

        access_log  /usr/local/var/log/nginx/access.log;
        error_log   /usr/local/var/log/nginx/error.log debug;
        log_not_found off;
        root    /Users/RobDee/workspace/beer;

        location /.htpasswd
        {
            return 403;
        }
        location ~ \.css {
            root /Users/RobDee/workspace/beer/web;
            expires max;
        }

        location ~* \.(jpg|jpeg|png|gif|ico|js|woff|woff2|ttf)$ {
            root /Users/RobDee/workspace/beer/web;
            access_log off;
            expires max;
        }

        location ~* \.(js|css)$ {
            expires 1y;
            log_not_found off;
        }

        location /
        {
            root /Users/RobDee/workspace/beer/web;
            try_files $uri $uri/ /app_dev.php$is_args$args;
            index app_dev.php;
        }

        location ~ \.php$ {
            root /Users/RobDee/workspace/beer/web;
            fastcgi_pass   127.0.0.1:9003;
            fastcgi_index  app_dev.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        rewrite ^/assets/([a-z\-]+)-([a-z0-9]+).(css|js) /assets/$1.$3;

    }

    include servers/*;
}
Pothi Kalimuthu
  • 5,734
  • 2
  • 24
  • 37