title: How to set up Nginx correctly when 403 Forbidden on CentOS 7?
body: <p>On CentOS 7</p>
  
  <p><code>/etc/hosts</code>:</p>
  
  <pre><code>127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  
  192.168.0.1  app1
  </code></pre>
  
  <p>Installed Nginx from package:</p>
  
  <pre><code>yum install nginx
  </code></pre>
  
  <p>In <code>/etc/nginx/nginx.conf</code>:</p>
  
  <pre><code># ...
  include /etc/nginx/conf.d/*.conf;
  
  server {
      listen       80 default_server;
      listen       [::]:80 default_server;
      server_name  _;
      root         /usr/share/nginx/html;
  
      # Load configuration files for the default server block.
      include /etc/nginx/default.d/*.conf;
  
      location / {
      }
  
      error_page 404 /404.html;
          location = /40x.html {
      }
  
      error_page 500 502 503 504 /50x.html;
          location = /50x.html {
      }
  }
  # ...
  </code></pre>
  
  <p>Created a new file under <code>/etc/nginx/sites-available/</code> named <code>myapp</code>:</p>
  
  <pre><code>upstream app {
      # Path to Unicorn SOCK file, as defined previously
      server unix:///home/deploy/myapp/tmp/sockets/unicorn.sock;
  }
  
  server {
      listen 192.168.0.1:80;
      server_name app1;
  
      # Application root, as defined previously
      root /home/deploy/myapp/public;
  
      try_files $uri/index.html $uri @app;
  
      location @app {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_pass http://app;
      }
  
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
  }
  </code></pre>
  
  <p>Link it to <code>/etc/nginx/sites-enabled/</code>:</p>
  
  <pre><code>cd /etc/nginx/sites-enabled/
  ln -s ../sites-available/myapp
  </code></pre>
  
  <p>Restart nginx:</p>
  
  <pre><code>service nginx restart
  </code></pre>
  
  <p>Then try to access url:</p>
  
  <pre><code>curl 192.168.0.1
  </code></pre>
  
  <p>Got error:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;&lt;title&gt;403 Forbidden&lt;/title&gt;&lt;/head&gt;
  &lt;body bgcolor="white"&gt;
  &lt;center&gt;&lt;h1&gt;403 Forbidden&lt;/h1&gt;&lt;/center&gt;
  &lt;hr&gt;&lt;center&gt;nginx/1.10.2&lt;/center&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I removed default <code>index.html</code> file under <code>/usr/share/nginx/html</code> path, so it got 403 Forbidden.</p>
  
  <p>Nginx error log <code>/var/log/nginx/error.log</code>:</p>
  
  <pre><code>2017/07/25 03:35:59 [error] 8200#0: *1 directory index of "/usr/share/nginx/html/" is forbidden, client: 192.168.0.2, server: _, request: "GET / HTTP/1.1", host: "192.168.0.1"
  </code></pre>
  
  <p>Why it accessed default <code>/usr/share/nginx/html/</code> path, but not new added <code>myapp</code> under <code>/etc/nginx/sites-enabled/</code> directory?</p>