Split nginx conf files for each virtual server

Hi, i noticed that nginx behave differently on virtualmin installed with LEMP bundle and LAMP bundle, then using nginx to replace apache.

With LEMP bundle, each virtual server have its own conf file on sites-available and sites-enabled folder, and virtualmin can select those file to edit from nginx webserver > Edit Configuration Files menu.

With LAMP bundle, converting to nginx using this guide I am running these commands

service httpd stop ; chkconfig httpd off
yum install nginx
systemctl start nginx
systemctl enable nginx
yum install wbm-virtualmin-nginx wbm-virtualmin-nginx-ssl

nginx will default to edit /etc/nginx/nginx.conf. I can change it’s behavior from nginx module config (cog button) > File or directory for new virtual hosts and Directory for links to new virtual host files and restarting nginx.

Then, when I created new virtual server from virtualmin menu, both sites-available and sites-enabled confs exist, but nginx module does not detect it on nginx webserver menu, only previously made nginx webserver (that is configured on nginx.conf file) detected. The virtual server created after modifying nginx module config cannot be accessed, it is redirected to default server.

How to make nginx detect sites-available and sites-enabled folders too?

UPDATE:
I added include /etc/nginx/sites-enabled/*; on nginx.conf on http block, and I can access the index.html on https protocol, but using http://test.my.site is redirected to https://my.site.
Let’s encrypt can obtain certificate just fine, though. But I still unable to access index.html file from http://test.my.site
UPDATE #2:
It seems like I cannot access http://test.my.site because of browser cache. Opening http://test.my.site from incognito window open index.html just fine

Here is nginx conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# BROTLI compression
#load_module modules/ngx_http_brotli_filter_module.so;
#load_module modules/ngx_http_brotli_static_module.so;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 200M;


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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

    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 = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#        location = /404.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#        location = /50x.html {
#        }
#    }

	server_names_hash_bucket_size 128;
	server {
		server_name [REDACTED].org www.[REDACTED].org;
		listen [REDACTED];
		root /home/[REDACTED]/public_html;
		index index.php index.htm index.html;
		access_log /var/log/virtualmin/[REDACTED].org_access_log;
		error_log /var/log/virtualmin/[REDACTED].org_error_log;
		fastcgi_param GATEWAY_INTERFACE CGI/1.1;
		fastcgi_param SERVER_SOFTWARE nginx;
		fastcgi_param QUERY_STRING $query_string;
		fastcgi_param REQUEST_METHOD $request_method;
		fastcgi_param CONTENT_TYPE $content_type;
		fastcgi_param CONTENT_LENGTH $content_length;
		fastcgi_param SCRIPT_FILENAME /home/[REDACTED]/public_html$fastcgi_script_name;
		fastcgi_param SCRIPT_NAME $fastcgi_script_name;
		fastcgi_param REQUEST_URI $request_uri;
		fastcgi_param DOCUMENT_URI $document_uri;
		fastcgi_param DOCUMENT_ROOT /home/[REDACTED]/public_html;
		fastcgi_param SERVER_PROTOCOL $server_protocol;
		fastcgi_param REMOTE_ADDR $remote_addr;
		fastcgi_param REMOTE_PORT $remote_port;
		fastcgi_param SERVER_ADDR $server_addr;
		fastcgi_param SERVER_PORT $server_port;
		fastcgi_param SERVER_NAME $server_name;
		fastcgi_param PATH_INFO $fastcgi_path_info;
		fastcgi_param HTTPS $https;
		location ~ \.php(/|$) {
			try_files $uri $fastcgi_script_name =404;
			fastcgi_pass unix:/var/php-nginx/161863766424443.sock/socket;
		}
		location / {
			try_files $uri $uri/ /index.php?$args;
		}

		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		listen [REDACTED]:443 ssl http2;
		ssl_certificate /home/[REDACTED]/ssl.combined;
		ssl_certificate_key /home/[REDACTED]/ssl.key;
		fastcgi_read_timeout 60;
	}
	server {
		server_name ns.[REDACTED].org www.ns.[REDACTED].org;
		listen [REDACTED];
		root /home/ns/public_html;
		index index.php index.htm index.html;
		access_log /var/log/virtualmin/ns.[REDACTED].org_access_log;
		error_log /var/log/virtualmin/ns.[REDACTED].org_error_log;
		fastcgi_param GATEWAY_INTERFACE CGI/1.1;
		fastcgi_param SERVER_SOFTWARE nginx;
		fastcgi_param QUERY_STRING $query_string;
		fastcgi_param REQUEST_METHOD $request_method;
		fastcgi_param CONTENT_TYPE $content_type;
		fastcgi_param CONTENT_LENGTH $content_length;
		fastcgi_param SCRIPT_FILENAME /home/ns/public_html$fastcgi_script_name;
		fastcgi_param SCRIPT_NAME $fastcgi_script_name;
		fastcgi_param REQUEST_URI $request_uri;
		fastcgi_param DOCUMENT_URI $document_uri;
		fastcgi_param DOCUMENT_ROOT /home/ns/public_html;
		fastcgi_param SERVER_PROTOCOL $server_protocol;
		fastcgi_param REMOTE_ADDR $remote_addr;
		fastcgi_param REMOTE_PORT $remote_port;
		fastcgi_param SERVER_ADDR $server_addr;
		fastcgi_param SERVER_PORT $server_port;
		fastcgi_param SERVER_NAME $server_name;
		fastcgi_param PATH_INFO $fastcgi_path_info;
		fastcgi_param HTTPS $https;
		location ~ \.php(/|$) {
			try_files $uri $fastcgi_script_name =404;
			fastcgi_pass unix:/var/php-nginx/16186691479333.sock/socket;
		}
		location / {
			try_files $uri $uri/ /index.php?$args;
		}
		fastcgi_split_path_info ^(.+\.php)(/.+)$;
		listen [REDACTED]:443 ssl http2;
		ssl_certificate /home/ns/ssl.combined;
		ssl_certificate_key /home/ns/ssl.key;
	}
}

add /etc/nginx/sites-enabled/*; on /etc/nginx/nginx.conf on http block so it looks like this

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    client_max_body_size 200M;


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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

####### this is the solution #########
	include /etc/nginx/sites-enabled/*;
####### this is end of the solution #########

    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 = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
	server_names_hash_bucket_size 128;
}

Please give feedback if there is better configuration for nginx.conf

This topic was automatically closed 8 days after the last reply. New replies are no longer allowed.