Setting Nginx configuration for CentOS VPS 6, 7

0
393

Nginx configuration file to VPS Linux CentOS live copy paste, lived. This configuration template can be applied after installing Nginx on VPS Cloud Server.

Install Nginx VPS Centos

Configuring Nginx

it’s at:/etc/nginx/nginx.conf

user              nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  30;
    # Untuk perfoma nginx terbaik
    gzip on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
    #gzip  on;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf

    # comment default nginx konfigurasi dan pakai konfig site-enable
    # include /etc/nginx/conf.d/*.conf;

    # enabled sites - enable server or virtual host block
    include /etc/nginx/sites-enabled/*;

}

Further reading: how Server Settings Block Nginx 6 CentOS VPS.

Add domains in Nginx

Example, the following Web.config to idnetter.com, create a file URsshagan.net in conf folder/etc/nginx/sites-enabled/.

vi /etc/nginx/sites-available/sshagan.net.conf

Its Contents

server {
        listen 80;
        server_name sshagan.net;

        # If use SSL to eliminate the following comments
        # listen 443 ssl;
        # ssl_certificate /etc/nginx/ssl/sshagan.net.crt;
        # ssl_certificate_key /etc/nginx/ssl/sshagan.net.key;

        client_max_body_size 5m;
        client_body_timeout 60;

        access_log /var/www/html/sshagan.net/access.log;
        error_log /var/www/html/sshagan.net/error.log error;

        root /var/www/html/sshagan.net/;
        index  index.html index.php;

        ### root directory ###
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        ### security ###
        error_page 403 =404;
        location ~ /\. { access_log off; log_not_found off; deny all; }
        location ~ ~$ { access_log off; log_not_found off; deny all; }
        location ~* wp-admin/includes { deny all; }
        location ~* wp-includes/theme-compat/ { deny all; }
        location ~* wp-includes/js/tinymce/langs/.*\.php { deny all; }
        location /wp-includes/ { internal; }
        location ~* wp-config.php { deny all; }
        location ~* ^/wp-content/uploads/.*.(html|htm|shtml|php)$ {
                types { }
                default_type text/plain;
        }

        ### disable logging ###
        location = /robots.txt { access_log off; log_not_found off; }
        location = /favicon.ico { access_log off; log_not_found off; }

        ### caches ###
        location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max; }
        location ~* \.(woff|svg)$ { access_log off; log_not_found off; expires 30d; }
        location ~* \.(js)$ { access_log off; log_not_found off; expires 7d; }

        ### php block ###
        location ~ \.php?$ {
                try_files $uri =404;
                include fastcgi_params;
                fastcgi_pass unix:/var/run/gateaway.socket;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_intercept_errors on;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                #Prevent version info leakage
                fastcgi_hide_header X-Powered-By;
        }
}

 

NO COMMENTS

LEAVE A REPLY