Nginx adapter on windows

1. Caddy version (caddy version):

Caddy 2.2.1

2. How I run Caddy:

Windows 10

a. System environment:

Windows 10

b. Command:

 .\caddy_windows_amd64_custom.exe adapt --config .\nginx.conf --adapter nginx

c. Service/unit/compose file:

N/A

d. My complete Caddyfile or JSON config:

`
#D I E T - P I
# /etc/nginx/nginx.conf

user www-data;

# As a thumb rule: One per CPU.
worker_processes 4;

pid /run/nginx.pid;

# Load dynamic modules
include /etc/nginx/modules-enabled/*.conf;

# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 100;

events {
	worker_connections 50;
}

error_log /var/log/nginx/error.log;



http {

	charset utf-8;

	# + Nginx - To avoid 2MB upload error: https://github.com/MichaIng/DietPi/issues/546
	client_max_body_size 2047M;

	# Upstream to abstract back-end connection(s) for PHP
	upstream php {
		server unix:/run/php/php7.3-fpm.sock;
	}

	# Set the mime-types via the mime.types external file
	include mime.types;

	# And the fallback mime-type
	default_type application/octet-stream;

	# Click tracking!
	access_log off;

	# Hide Nginx version
	server_tokens off;

	# ~2 seconds is often enough for HTML/CSS, but connections in
	# Nginx are cheap, so generally it's safe to increase it
	keepalive_timeout 2;

	# You usually want to serve static files with Nginx
	sendfile on;

	tcp_nopush on; # off may be better for Comet/long-poll stuff
	tcp_nodelay off; # on may be better for Comet/long-poll stuff

	server_name_in_redirect off;
	types_hash_max_size 2048;
	server_names_hash_bucket_size  64;

	gzip off;
	gzip_http_version 1.0;
	gzip_comp_level 1;
	gzip_min_length 512;
	gzip_buffers 4 8k;
	gzip_proxied any;
	gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon;

	# This should be turned on if you are going to have pre-compressed copies (.gz) of
	# static files available. If not it should be left off as it will cause extra I/O
	# for the check. It would be better to enable this in a location {} block for
	# a specific directory:
	#gzip_static on;

	gzip_disable "msie6";
	gzip_vary on;
	
	#security options applies to all sites
		
    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1;mode=block";
    add_header Feature-Policy "geolocation none;midi none;notifications none;push none;sync-xhr none;microphone none;camera none;magnetometer none;gyroscope none;speaker self;vibrate none;fullscreen self;payment none;";
######### for wordpress
map $uri $blogname{
    ~^(?P<blogpath>/[^/]+/)files/(.*)       $blogpath ;
}
 
map $blogname $blogid{
    default -999;
}
#########	
	
	
#####a#####
	server {
	
	ssl_certificate /etc/nginx/ssl/cert.pem;
    ssl_certificate_key /etc/nginx/ssl/key.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;
	
#	root /var/www/a/ftp_tmp;
	root /var/www/a;
	server_name www.a.com;
	listen 443 ssl http2;
	keepalive_timeout 70;
	index index.php;
	location ~ \.php$ {
	fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
	fastcgi_index  index.php;
	include /etc/nginx/fastcgi_params;
	access_log  /var/log/nginx/a/main.access.log;
        error_log  /var/log/nginx/a/main.error.log;
	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         $document_root$fastcgi_script_name;
	fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
	fastcgi_param   PATH_INFO               $fastcgi_path_info;
	fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;
	fastcgi_param   REQUEST_URI             $request_uri;
	fastcgi_param   DOCUMENT_URI            $document_uri;
	fastcgi_param   DOCUMENT_ROOT           $document_root;
	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;
    }
	}	
###### b#####
	server {
	
	ssl_certificate /etc/nginx/ssl/cert2.pem;
    ssl_certificate_key /etc/nginx/ssl/key2.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;
	
	root /var/www/b;
	server_name b.com ;
	listen 443 ssl http2;
	keepalive_timeout 70;
	access_log /var/log/nginx/b/main.access.log;
	error_log /var/log/nginx/b/main.error.log;
	}
###### c#####	

server {
    ssl_certificate /etc/nginx/ssl/cert3.pem;
    ssl_certificate_key /etc/nginx/ssl/key3.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;
    keepalive_timeout 70;
    access_log /var/log/nginx/c.log;
    error_log /var/log/nginx/c.log;
    listen 443 ssl http2;
    server_name c.com;
    root /var/www/c;
    index index.php;
	
    location ~ ^(/[^/]+/)?files/(.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
        access_log off;     log_not_found off; expires max;
    }
 
    #avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /var/www/c/wp-content/blogs.dir ;
        access_log off;     log_not_found off; expires max;
    }
 
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }
 
    location / {
        try_files $uri $uri/ /index.php?$args ;
    }
 
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_param HTTPS on;
	fastcgi_index index.php;
	include fastcgi.conf;
    }

	}
#####d#######
	server {	
	ssl_certificate /etc/nginx/ssl/cert4.pem;
    ssl_certificate_key /etc/nginx/ssl/key4.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;	
	root /var/www/d;
	server_name d.com;
	listen 443 ssl http2;
	keepalive_timeout 70;
	index index.php;
	access_log /var/log/nginx/d/main.access.log;
	error_log /var/log/nginx/d/main.error.log;

	location ~ ^(/[^/]+/)?files/(.+) {
        try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
        access_log off;     log_not_found off; expires max;
    }
 
    #avoid php readfile()
    location ^~ /blogs.dir {
        internal;
        alias /var/www/d/wp-content/blogs.dir ;
        access_log off;     log_not_found off; expires max;
    }
 
    if (!-e $request_filename) {
        rewrite /wp-admin$ $scheme://$host$request_uri/ permanent;
        rewrite ^(/[^/]+)?(/wp-.*) $2 last;
        rewrite ^(/[^/]+)?(/.*\.php) $2 last;
    }
 
    location / {
        try_files $uri $uri/ /index.php?$args ;
    }
 
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_param HTTPS on;
	fastcgi_index index.php;
	include fastcgi.conf;
    }
	}
#####e#####
	server {
	
	ssl_certificate /etc/nginx/ssl/cert5.pem;
    ssl_certificate_key /etc/nginx/ssl/key5.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;
	
	root /var/www/e;
	server_name e.com;
	listen 443 ssl http2;
	keepalive_timeout 70;
	index index.html;
	
	access_log /var/log/nginx/e/access.log;
	error_log /var/log/nginx/e/error.log;
	
	location ~ \.(jpg|jpeg|gif|png|css|js|ico|svg|eot|ttf|woff|woff2|otf)$ {
        access_log        off;
        expires           30d;
    }

    location ~ \.php$ {
        fastcgi_pass    unix:/var/run/php/php7.3-fpm.sock;
        fastcgi_index   index.php;
        include         fastcgi.conf;
        fastcgi_param   HTTPS on;
    }

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

    location ^~ /bl-content/databases/ { deny all; }
    location ^~ /bl-content/workspaces/ { deny all; }
    location ^~ /bl-content/pages/ { deny all; }
    location ^~ /bl-kernel/*.php { deny all; }
	}
	
######f########
	server {
	ssl_certificate /etc/nginx/ssl/cert6.pem;
    ssl_certificate_key /etc/nginx/ssl/key6.pem;
    ssl_client_certificate /etc/nginx/ssl/origin-pull-ca.pem;
    ssl_verify_client on;
	listen 443 ssl http2;
    keepalive_timeout 70;
    server_name f.com;
	autoindex off;
	client_max_body_size 15M;
	client_body_buffer_size 128k;
	index doku.php;
	access_log  /var/log/nginx/f/wiki.access.log;
	error_log  /var/log/nginx/f/wiki.error.log;
	root /var/www/f;
	location / {
	try_files $uri $uri/ @dokuwiki;
	}

  location ~ ^/lib.*\.(gif|png|ico|jpg)$ {
    expires 30d;
  }

  location = /robots.txt  { access_log off; log_not_found off; }
  location = /favicon.ico { access_log off; log_not_found off; }
  location ~ /\.          { access_log off; log_not_found off; deny all; }
  location ~ ~$           { access_log off; log_not_found off; deny all; }

  location @dokuwiki {
    rewrite ^/_media/(.*) /lib/exe/fetch.php?media=$1 last;
    rewrite ^/_detail/(.*) /lib/exe/detail.php?media=$1 last;
    rewrite ^/_export/([^/]+)/(.*) /doku.php?do=export_$1&id=$2 last;
    rewrite ^/(.*) /doku.php?id=$1 last;
  }

  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass   unix:/var/run/php/php7.3-fpm.sock;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include /etc/nginx/fastcgi_params;
    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_intercept_errors        on;
    fastcgi_ignore_client_abort     off;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
  }

  location ~ /(data|conf|bin|inc)/ {
    deny all;
  }

  location ~ /\.ht {
    deny  all;
  }
        }
########Pi-Hole########	
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    server_name _;
    autoindex off;

    index pihole/index.php index.php index.html index.htm;

    location / {
        expires max;
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_param FQDN true;
        
    }

    location /*.js {
        index pihole/index.js;
        #auth_basic "PiHole Admin"; # For Basic Auth
        #auth_basic_user_file /etc/nginx/.htpasswd; # For Basic Auth
    }

    location /admin {
        root /var/www/html;
        index index.php index.html index.htm;
        #auth_basic "PiHole Admin"; # For Basic Auth
        #auth_basic_user_file /etc/nginx/.htpasswd; # For Basic Auth
    }

    location ~ /\.ht {
        deny all;
    }
}
####new website####

###xxxxxxx####end of http tag below, all above this####xxxxxxx###
}

`

3. The problem I’m having:

I am trying to convert my nginx.conf to caddy json

4. Error messages and/or full log output:

adapt: parsing: Could not import \etc\nginx\mime.types: open \etc\nginx\mime.types: The system cannot find the path specified.

5. What I already tried:

I attempted to try on linux but could not figure it out so tried the windows version with the nginx-adapter ticket and got the above error, I assume this is because the adapter is hard coded for linux only

6. Links to relevant resources:

Hey, Jake, welcome around :wave:

You say you tried to use it on Linux but couldn’t figure it out. What issues did you have on Linux? Although the current implementation can recognize the include directive, it isn’t implemented fully and an open issue exists for full support of the directive.

Also note your nginx config file contains these lines:

include /etc/nginx/modules-enabled/*.conf;
...
include /etc/nginx/fastcgi_params;

The paths here are hard-coded for *nix systems. It’s true that the current implementation covers only *nix systems though.

Hey Mohammed, my idea was to convert it then go through the file and double check everything against the docs rather then starting from scratch. i got a vm running ubuntu and have installed go, then i think i build caddy correctly so i tried to run caddy adapt --config nginx.conf --adapter nginx agaist the caddy program i made but is said caddy was not a valid command, so i am missing something somewhere.

The command fails because the nginx adapter isn’t included as part of the standard Caddy build. You will have to use xcaddy to build a custom executable of Caddy containing the the nginx adapter as follows:

xcaddy build --with github.com/caddyserver/nginx-adapter
1 Like

* or get it from the Download page :smiley:

im pretty sure i did that but i’ll try again today

Well i got a bit further, i have found out the i have to type “go run” to run the file and i downloaded the file with the nginx adapter but it still does not like it

I don’t know what “caddy_custom” is, so let’s start over…

Download Caddy with the nginx adapter plugged in: https://caddyserver.com/api/download?os=windows&arch=amd64&p=github.com%2Fcaddyserver%2Fnginx-adapter

Then open your Windows command prompt to the folder where it is, and run caddy list-modules. What is the full output?

Hi Matt,
Caddy_custom is from the download of the website with the nginx adapter, i just made the name shorter(it was caddy_linux_amd64_custom)

Here is the output from your link:
PS G:\Downloads> & '.\caddy_windows_amd64_custom(1).exe' list-modules admin.api.load admin.api.metrics caddy.adapters.caddyfile caddy.adapters.nginx caddy.listeners.tls caddy.logging.encoders.console caddy.logging.encoders.filter caddy.logging.encoders.filter.delete caddy.logging.encoders.filter.ip_mask caddy.logging.encoders.json caddy.logging.encoders.logfmt caddy.logging.encoders.single_field caddy.logging.writers.discard caddy.logging.writers.file caddy.logging.writers.net caddy.logging.writers.stderr caddy.logging.writers.stdout caddy.storage.file_system http http.authentication.hashes.bcrypt http.authentication.hashes.scrypt http.authentication.providers.http_basic http.encoders.gzip http.encoders.zstd http.handlers.acme_server http.handlers.authentication http.handlers.encode http.handlers.error http.handlers.file_server http.handlers.headers http.handlers.map http.handlers.metrics http.handlers.push http.handlers.request_body http.handlers.reverse_proxy http.handlers.rewrite http.handlers.static_response http.handlers.subroute http.handlers.templates http.handlers.vars http.matchers.expression http.matchers.file http.matchers.header http.matchers.header_regexp http.matchers.host http.matchers.method http.matchers.not http.matchers.path http.matchers.path_regexp http.matchers.protocol http.matchers.query http.matchers.remote_ip http.matchers.vars http.matchers.vars_regexp http.reverse_proxy.selection_policies.first http.reverse_proxy.selection_policies.header http.reverse_proxy.selection_policies.ip_hash http.reverse_proxy.selection_policies.least_conn http.reverse_proxy.selection_policies.random http.reverse_proxy.selection_policies.random_choose http.reverse_proxy.selection_policies.round_robin http.reverse_proxy.selection_policies.uri_hash http.reverse_proxy.transport.fastcgi http.reverse_proxy.transport.http http.reverse_proxy.transport.http_ntlm pki tls tls.certificates.automate tls.certificates.load_files tls.certificates.load_folders tls.certificates.load_pem tls.handshake_match.sni tls.issuance.acme tls.issuance.internal tls.issuance.zerossl tls.stek.distributed tls.stek.standard

This topic was automatically closed after 30 days. New replies are no longer allowed.