title: Nginx serve static file and got 403 forbidden
body: <p>Just want to help somebody out. yes ,you just want to serve static file using nginx, and you got everything right in <strong>nginx.conf</strong>:</p>
  
  <pre><code>location /static {
         autoindex on;
         #root /root/downloads/boxes/;
         alias /root/downloads/boxes/;
        }
  </code></pre>
  
  <p>But , in the end , you failed. You got "403 forbidden" from browser...</p>
  
  <p>----------------------------------------<strong>The Answer Below:</strong>----------------------------------------</p>
  
  <p>The Solution is very Simple:</p>
  
  <hr>
  
  <p><strong>Way 1 : Run nginx as the user as the '/root/downloads/boxes/' owner</strong></p>
  
  <p>In <strong>nginx.conf</strong> : </p>
  
  <pre><code>＃user  nobody;
  worker_processes  1;
  
  #error_log  logs/error.log;
  #error_log  logs/error.log  notice;
  </code></pre>
  
  <p>YES, in the first line "<strong>＃user  noboy;</strong>" , just delete "<strong>#</strong>" , and change "<strong>nobody</strong>" to your own username in Linux/OS X, i.e change to "<strong>root</strong>" for test. The restart nginx. </p>
  
  <p><strong>Attention</strong> , You'd better not run <strong>nginx</strong> as <strong>root</strong>! Here just for testing, it's dangerous for the Hacker. </p>
  
  <p>For more reference , see <a href="http://nicholasorr.com/blog/2008/07/22/nginx-engine-x-what-a-pain-in-the-bum/" rel="noreferrer">nginx (engine X) – What a Pain in the BUM! [13: Permission denied]</a></p>
  
  <hr>
  
  <p><strong>Way 2 : Change '/root/downloads/boxes/' owner to 'www-data' or 'nobody'</strong></p>
  
  <p>In <strong>Terminal</strong>:</p>
  
  <pre><code>ps aux | grep nginx
  </code></pre>
  
  <p>Get the username of running nginx . It should be <strong>'www-data'</strong> or <strong>'nobody'</strong> determined by the version of nginx. Then hit in Terminal(use <strong>'www-data'</strong> for example):</p>
  
  <pre><code>chown -R www-data:www-data /root/downloads/boxes/
  </code></pre>
  
  <p>------------------------------<strong>One More Important Thing Is:</strong>------------------------------</p>
  
  <p>These parent directories <strong>"/"</strong>, <strong>"/root"</strong>, <strong>"/root/downloads"</strong> should give the execute(x) permission to <strong>'www-data'</strong> or <strong>'nobody'</strong>. i.e.</p>
  
  <pre><code>ls -al /root
  chmod o+x /root
  chmod o+x /root/downloads
  </code></pre>
  
  <p>For more reference , see <a href="http://nginxlibrary.com/403-forbidden-error/" rel="noreferrer">Resolving "403 Forbidden" error</a> and <a href="https://stackoverflow.com/questions/6795350/nginx-403-forbidden-for-all-files">Nginx 403 forbidden for all files</a></p>