Convert nginx to Caddy2 config 2

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

a. System environment:

CentOS7

b. Command:

systemctl start caddy

c. Service/unit/compose file:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target network-online.target
Requires=network-online.target

[Service]
Type=notify
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile --force
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

d. My complete Caddy config:

(handle_info_log) {
	log {
		output file /var/log/caddy/info.log {
			roll_size 100MiB

			roll_keep_for 30d

			#roll_local_time
		}

		format console {
			time_format wall

			time_local
		}

		level INFO
	}
}

(handle_error_log) {
	log {
		output file /var/log/caddy/error.log {
			roll_size 30MiB

			roll_keep_for 30d

			#roll_local_time
		}

		format console {
			time_format wall

			time_local
		}

		level ERROR
	}
}

(handle_errors) {
	handle_errors {
		rewrite * /{err.status_code}.html

		file_server
	}
}

www.tigerice.cn {
	encode gzip

	import handle_errors

	import handle_info_log

	import handle_error_log

	handle_path /merchant/* {

		root * /opt/twomicro_v2/web/merchant

		file_server
	}


	handle_path /formmaking/* {
		root * /opt/twomicro_v2/web/dist

		file_server
	}

	@nacos path /nacos /nacos*
	#@nacos path /nacos*
	reverse_proxy @nacos {
		header_up Host {http.reverse_proxy.upstream.hostport}
		header_down Access-Control-Allow-Origin *
		header_down Access-Control-Allow-Methods GET,POST,OPTIONS
		header_down Access-Control-Allow-Headers X-Requested-With
		to http://localhost:8848
	}

	reverse_proxy /demo-framework/* {
		header_down Access-Control-Allow-Origin *
		header_down Access-Control-Allow-Methods GET,POST,OPTIONS
		header_down Access-Control-Allow-Headers X-Requested-With
		to localhost:8081
	}

	reverse_proxy /demo-example/* {
		to localhost:8081
	}

	@logsurl {
		path_regexp logsurl ^/demo-log/(.*)$
	}
	rewrite @logsurl /demo-framework/{http.regexp.logsurl.1}

	@upmsurl {
		path_regexp upmsurl ^/demo-upms/(.*)$
	}
	rewrite @upmsurl /demo-framework/{http.regexp.upmsurl.1}

	@systemsurl {
		path_regexp systemsurl ^/demo-system/(.*)$
	}
	rewrite @systemsurl /demo-framework/{http.regexp.systemsurl.1}

	@workflowsurl {
		path_regexp workflowsurl ^/demo-workflow/(.*)$
	}
	rewrite @workflowsurl /demo-framework/{http.regexp.workflowsurl.1}
}

3. The problem I’m having:

how to convert nginx conf to caddy2 config

nginx conf

              if ($request_method = 'OPTIONS') {
                  add_header 'Access-Control-Max-Age' 1728000;
                  add_header 'Content-Type' 'text/plain; charset=utf-8';
                  add_header 'Content-Length' 0;
                  return 204;
              }
              
              
              keepalive_timeout  600;
              proxy_send_timeout 1800; 
              proxy_read_timeout 1800; 

4. Error messages and/or full log output:

Paste logs/commands/output here.
USE THE PREVIEW PANE TO MAKE SURE IT LOOKS NICELY FORMATTED.

5. What I already tried:

caddy2 config

 header_down  'Access-Control-Max-Age' 1728000

 header_down  'Content-Type' 'text/plain; charset=utf-8'

 header_down  'Content-Length' 0

 respond 204

6. Links to relevant resources:

this is nginx global configuration, how to convert it to caddy2 config ?

      client_max_body_size 100M;
      include     mime.types;
      default_type  application/octet-stream;
      sendfile      on;

      keepalive_timeout  300;
      proxy_read_timeout 300;
      proxy_send_timeout 300;
      send_timeout 300;
      proxy_connect_timeout 300;
      add_header Cache-Control no-cache;

      gzip    on;
      gzip_min_length   256k;
      gzip_buffers   4 512k;
      gzip_comp_level 2;
      gzip_http_version  1.1;
      gzip_types   text/plain application/x-JavaScript text/css  application/xml;
      gzip_disable "MSIE [1-6]\.";
      gzip_vary on;
      
      fastcgi_connect_timeout 300;
      fastcgi_send_timeout 300;
      fastcgi_read_timeout 300;
      fastcgi_buffer_size 64k;
      fastcgi_buffers 4 64k;
      fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 128k;
      

Hi,

I think our helpers are a bit busy and tired lately <3

We will be more likely to help you if you fill out the template completely, especially section 5 – what have you already tried? It is hard to motivate oneself to do all the work for you without any effort on your part… thank you!

ok, thank you for your advice, i will fill out the section 5

1 Like