Thursday, December 27, 2018

Nginx on freeBSD: 2. Setting Up a server Block

cont... (Install Nginx on freeBSD)

Nginx on FreeBSD 11.2 has one server block enabled by default that is configured to serve documents out of a directory at /usr/local/www/nginx. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /usr/local/www/nginx, let's create a directory structure within /usr/local/www for our example.com site.

#sudo mkdir -p /usr/local/www/example.com/

Assign ownership of the directory to the www user:
#sudo chown -R www:www /usr/local/www/example.com

The permissions of your web root should be correct if you haven't modified your umask value, but you can make sure by typing:
# sudo chmod -R 755 /usr/local/www/example.com

# sudo ee /usr/local/www/example.com/index.html
<h1>Web example 1</h1>

Change the configuration
To find the cpu cores:
# sysctl hw.ncpu
# sudo ee /usr/local/etc/nginx/nginx.conf
(To load from the system backup
#sudo cp /usr/local/etc/nginx/nginx.conf-dist /usr/local/etc/nginx/nginx.conf)

/usr/local/letc/nginx/nginx.conf
user  www;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        access_log /var/log/nginx/example.com.access.log;
        error_log /var/log/nginx/example.com.error.log;
        listen       80;
        server_name  example.com www.example.com;

        location / {
            root   /usr/local/www/example.com;
            index  index.html index.htm;
        }

    }

}

# sudo nginx -t
# sudo service nginx reload

For port check: Port firewall/block checking points

Funny mistakes:
1. You need to login from outside network to login with the public IP!
2. Make sure your port is open.

No comments:

Post a Comment