0

I've got nginx 1.8.1 on a server running FreeBSD 10.2. I'm trying to serve files from a subdirectory of /usr/local/www/ (which is default on FreeBSD), but for some reason, requests to the files I'm trying to access always result in a 404.

The file specifically is /usr/local/www/example.com/pubic_html/index.html, with /usr/local/www recursively owned by www:www, the user and group of the nginx worker process, with permissions recusrively set to 755.

My server block, in /usr/local/etc/nginx/sites-available/example.com:

server {
    listen 80 default;
    server_name example.com www.example.com;

    root /usr/local/www/example.com/public_html;

    location /app {
        proxy_pass http://localhost:8080;
    }

    location / {
        try_files $uri =404;
    }
}

I'm honestly puzzled by this. I haven't had this issue on Linux with Nginx or Apache, and I haven't had this issue working with Apache on FreeBSD before, either. How can I get nginx to serve this static file?

EDIT: while requests for static files don't result in anything being logged to my nginx-error.log, it does result in normal 404 hit records in the nginx-access.log.

13
  • nginx error.log usually contains the answer.
    – citrin
    Nov 17, 2016 at 0:30
  • There's nothing at all about these 404s in nginx-error.log. The entries in nginx-access.log aren't informative.
    – Jules
    Nov 17, 2016 at 0:51
  • Did you try adding an index index.html? Also, unless you have a very good reason, www-data -being your webserver runtime user- should not have write permissions over the content it serves.
    – SYN
    Nov 17, 2016 at 1:03
  • @SYN this is just a test; I'm trying to get familiar with setting up nginx on FreeBSD. Normally I would put it to 644 with ownership by someuser:www-data. (And just to nitpick; www-data is www on FreeBSD).
    – Jules
    Nov 17, 2016 at 1:28
  • What is the URL you are trying to access? Nov 17, 2016 at 2:12

2 Answers 2

0

OK here's the thing: the default configurations for web servers in FreeBSD DO NOT use files inside the sites-available directory. Either you have to put the contents of the files inside /usr/local/etc/nginx/nginx.conf or add this line to /usr/local/etc/nginx/nginx.conf so that it will read files inside the sites-available directory:

# virtual hosting
include /usr/local/etc/nginx/sites-available/*.conf;

The same behavior also applies to Apache on FreeBSD. (Probably on other web servers as well, but I have experience only with these two.)

3
  • This line was in the default nginx.conf that was placed inside the initial pkg install. I've even intentionally missed semicolons in site config files to make sure it was really reading them; nginx -s reload errored (as expected) when this happened.
    – Jules
    Nov 17, 2016 at 15:13
  • This was not like my case. Anyway you can put the content right inside nginx.conf instead to make sure that the configurations really work. Nov 18, 2016 at 5:36
  • I have made sure the configuration really works -- there's literally no need at this point to verify that.
    – Jules
    Nov 18, 2016 at 5:51
0

Do you have other configuration files for Nginx?
I guess you have listen <ip> directive somewhere else so you should also use listen <ip> default_server or listen <ip>:80 default_server in your example.com configuration.

2
  • I have no other configuration files for nginx, and I'm confident this is the one being read; otherwise I wouldn't be able to do port forwarding on that web application at /app. I tried changing default to default_server, as well as explicitly writing the public IP of my server instead of just 80, but no dice.
    – Jules
    Nov 18, 2016 at 1:19
  • One more guess: have you tried try_files $uri $uri/ =404 or remove try_files ? I've tried your config, and it works with curl --resolve example.com:80:127.0.0.1 http://example.com/index.html but failed with curl --resolve example.com:80:127.0.0.1 http://example.com/ and try_files $uri $uri/ =404 or removing try_files fixed this too. Nov 18, 2016 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .