Nginx 404 针对 css js 文件

Nginx 404 针对 css js 文件

我无法在 MAC 上加载 css 和 js 文件。这些文件加载​​时出现 404 错误。在 Ubuntu 上,我只需在 nginx.conf 末尾添加以下内容,rewrite ^/assets/([a-z\-]+)-([a-z0-9]+).(css|js) /assets/$1.$3;它就可以正常工作。

但是我不知道把它放在 osx 的哪里,因为当我在 Ubuntu 上写它时,我得到了语法错误......

我的 nginx 配置文件如下:

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/*;

    }

答案1

重写指令只能在服务器、位置或“if”块的上下文中使用。例如,它不能在“http”块中使用。您必须在事件块或 http 块(与其他服务器块一起)中使用它。请查看我使用重写指令的地方。

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/*;
}

相关内容