2
votes

I've been banging around with the process of migrating a site from Apache to Nginx and I'm about to lose my mind. The virtual host doesn't want to serve static resources (css, js, etc.) and I can't seem to figure out why. The server block looks like this:

server {
  listen      443 default_server ssl;
  server_name dev.myproject.mydomain.net;
  root        /opt/dev/myproject;
  index       index.php;

  ssl_certificate      /etc/ssl/certs/server.crt;
  ssl_certificate_key  /etc/ssl/certs/server.pem;

  access_log /var/log/nginx/vh.project.dev.access.log;
  error_log  /var/log/nginx/vh.project.dev.error.log;

  location ~ ^/alias_name/(.*) {
    alias /opt/dev/myproject/www/$1;

    location ~ ^/alias_name/(.+\.php)$ {
      alias /opt/dev/myprojectp/www/$1;
      include /etc/nginx/conf/php;
    }
  }

  location ~ \.php$ {
    include /etc/nginx/conf/php;
  }

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

What am I missing? I know it's my inexperience with Nginx, but any advice would be much appreciated at this point.

Thanks.

UPDATE

This appears to be related to my alias which I was having trouble with before. If I point my document root to the alias location (/opt/dev/myprojectp/www), and try to render static content without the alias, it renders fine. As soon as I throw the alias in the URL...not so much.

2
Could you show the content of your "/etc/nginx/conf/php"?VBart

2 Answers

2
votes

Take from : http://kbeezie.com/view/nginx-configuration-examples/

# This block will catch static file requests, such as images, css, js
# The ?: prefix is a 'non-capturing' mark, meaning we do not require
# the pattern to be captured into $1 which should help improve performance
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
1
votes

Okay, so I may have found my own answer by brute force trial and error. This virtual host server block seems to serve PHP and static content properly:

server {
  listen      443 default_server ssl;
  server_name dev.myproject.mydomain.net;
  root        /opt/dev/project-root;
  index       index.php;

  ssl_certificate      /etc/ssl/certs/server.crt;
  ssl_certificate_key  /etc/ssl/certs/server.pem;

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

  location ~ ^/alias_name/(.+\.php)$ {
    alias /opt/dev/project-root/www/$1;
    include /etc/nginx/conf/php;
  }
  location ~ ^/alias_name/(.*) {
    alias /opt/dev/project-root/www/$1;
  }

  location ~ \.php$ {
    include /etc/nginx/conf/php;
  }
}

I don't know whether I'll bump into problems and I can't say that I fully understand the difference, but simply removing the nested location blocks seems to have done the trick.