Kamailio Asterisk Integration: Kamailio in Front
A side-by-side guide to Kamailio in front of Asterisk: dispatcher load balancing, failover, IP trust and matching pjsip.conf settings, from official docs.
By Arkay · Updated · Published

Why put Kamailio in front of Asterisk?
Kamailio handles SIP routing, load balancing, failover and IP trust lists, while Asterisk runs the calls. Putting Kamailio first lets one routing layer decide where each call goes. The Asterisk servers behind it then handle the call itself.
The Kamailio dispatcher module describes itself this way: "This module offers SIP load balancer functionality and it can be used as SIP traffic dispatcher." The full page is in the Kamailio dispatcher module documentation.
Each section below opens with a direct answer and then gives the detail. The settings are grouped by the question they answer, so you can jump to the part that matches your setup.
What does our side-by-side comparison cover?
We compiled a side-by-side map of the Kamailio and Asterisk settings used when Kamailio sits in front of Asterisk. Nothing here was tested. Every row comes from the official pages linked in this post, checked November 14, 2026.
The Kamailio rows come from the dispatcher and permissions module pages (stable, v6.1.x), and from the default kamailio.cfg on the master branch, labelled v6.2. The Asterisk rows come from the res_pjsip reference and the res_pjsip_endpoint_identifier_ip reference, generated from Asterisk branch 23.
| Job | Kamailio side | Asterisk side |
|---|---|---|
| Pick a server for each call | Dispatcher list file and ds_select_dst(set, alg[, limit]) | Not in the compiled note |
| Accept requests from a known IP | Permissions module: allow_source_address([group_id]) and the address table | Identify section: match takes IPs, hostnames, CIDR or masks |
| Match a request to an endpoint | ds_is_from_list checks whether a request came from a listed server | Identify section maps inbound traffic to an endpoint by source |
| Run the dialplan for inbound calls | Not in the compiled note | Endpoint context option (default default) |
| Handle NAT | WITH_NAT define in the default kamailio.cfg | Transport local_net, external_media_address and external_signaling_address; endpoint rewrite_contact and force_rport |
Read the table row by row. The Kamailio column controls which server gets a call and which addresses Kamailio accepts. The Asterisk column controls how Asterisk maps that traffic to an endpoint and how the endpoint handles media.
Where the compiled note has no entry, the cell says so. We did not fill those gaps from memory.
What goes in the dispatcher.list file?
The file holds one destination per line. The columns are setid, destination (a SIP URI), then flags, priority and attrs. The last three are optional. The attrs column takes name=value pairs separated by semicolons, such as duid=abc in the docs' example line.
By default, Kamailio reads /etc/kamailio/dispatcher.list, or /usr/local/etc/kamailio/dispatcher.list. The db_url parameter loads the list from a database instead. Its default is NULL, which disables database support.
The two lines below are examples only. Replace each placeholder with your own Asterisk server address. Both sit in set 1, and the two zeros fill the optional flags and priority columns.
1 sip:<asterisk-1-ip>:5060 0 0
1 sip:<asterisk-2-ip>:5060 0 0
Each Asterisk server gets its own line. Keep the set id the same for all servers that should share the same calls, so the selection function can choose between them.
How does Kamailio load balance calls across several Asterisk servers?
The dispatcher module picks one destination from a set for each call. The function ds_select_dst(set, alg[, limit]) takes a set id and an algorithm number. The docs' example balances across group 1 with round-robin, which is algorithm 4:
# round robin dispatching on gateways group '1'
ds_select_dst("1", "4");
In that line, "1" is the set id and "4" is round-robin. The selected address goes into $du, the dst_uri, which is the outbound proxy address. It is not visible in the SIP request, so the request URI does not change.
Mixing this up is an easy mistake. The ds_select_domain function works differently. It picks a destination and rewrites the host and port of the request URI. Choose the function based on whether you want the visible request URI to change.
The docs' example also replies 404 "No destination" when no address is found. It sets t_on_failure("RTF_DISPATCH"), so a failed attempt goes to a failure route. The next section covers that route.
How does Kamailio fail over when an Asterisk server is down?
Failover has two parts. The first is a failure route that asks for the next destination. The second is a set of health checks that can mark a server as down. The docs' example handles the first part. On a 500 reply or a branch timeout, the failure route calls ds_next_dst() and then relays the request again.
The dispatcher docs' example, written as kamailio.cfg routes:
route[DISPATCH] {
if(!ds_select_dst("1", "4")) {
send_reply("404", "No destination");
exit;
}
t_on_failure("RTF_DISPATCH");
route(RELAY);
exit;
}
failure_route[RTF_DISPATCH] {
if (t_is_canceled()) {
exit;
}
if (t_check_status("500") or (t_branch_timeout() and !t_branch_replied())) {
if(ds_next_dst()) {
t_on_failure("RTF_DISPATCH");
route(RELAY);
exit;
}
}
}
The route[DISPATCH] block picks a destination. The if line calls ds_select_dst("1", "4"), which picks from set 1 by round-robin. If no destination is found, the next line replies 404 "No destination" and exit ends the route.
The t_on_failure("RTF_DISPATCH") line names the failure route for this request. The route(RELAY) line relays the request. RELAY is the relay route from the default config. The last exit ends the route.
The failure_route[RTF_DISPATCH] block runs when the attempt fails. The first if exits when the call was canceled. The next if checks for a 500 reply or a branch timeout with no reply.
The inner if line calls ds_next_dst(), which takes the next destination and sets it as the dst_uri, the outbound proxy address. If a next destination exists, the route sets t_on_failure("RTF_DISPATCH") again, relays the request, and exits.
The full example is on the Kamailio dispatcher module documentation page.
The health checks use the ds_ping_method option, which defaults to OPTIONS. The ds_ping_interval option defaults to 0, which means no pinging. It is only used when the tm module is loaded. With the default of 0, Kamailio sends no pings, so the interval must be set above 0 before pings can mark a server.
The ds_probing_mode option decides which servers get tested. Mode 0, the default, tests only gateways in the PROBING state. Mode 1 tests all gateways.
The ds_mark_dst() function marks the last used destination. With no state given, it marks that destination inactive. The states are i (inactive), a (active), d (disabled) and t (trying), and p adds probing mode.
How do I make Kamailio trust traffic from Asterisk by IP address?
Load the permissions module through WITH_IPAUTH, then put the Asterisk addresses in the address table under group 1. The default configuration says the same in its comments: "add IP addresses with group id '1' to 'address' table".
With WITH_IPAUTH defined, the default config loads permissions.so. Its authorization route accepts non-REGISTER requests whose source passes the check:
if((!is_method("REGISTER")) && allow_source_address()) {
The function allow_source_address([group_id]) equals allow_address(group_id, "$si", "$sp"). If you leave out the group, it uses group 1. The check compares the source IP ($si) and source port ($sp) against the group's entries.
The address table has the columns grp, ip_addr, mask and port. A port value of 0 matches any port. If the permissions address_file parameter is set, it takes priority over the database, and addresses load from that file. The db_url parameter sets the database used by the allow_trusted and allow_address rules.
The dispatcher module has its own check. The ds_is_from_list([groupid [, mode [, uri]]]) function returns true if the source address or URI matches an address in the given group. Without a group id, or with -1, it checks all groups.
How does Asterisk accept calls from Kamailio without registration?
Asterisk can match an inbound request to an endpoint through an identify section, which uses the res_pjsip_endpoint_identifier_ip module. The identify section matches inbound traffic to an endpoint by criteria other than the From user. For a Kamailio peer, the criterion is the source address.
The match option, available since 12.2.0, takes a comma-separated list of IP addresses or hostnames. Entries may carry a subnet mask in CIDR or dotted-decimal form and an optional source port. The match_header option matches a SIP header, since 13.15.0 and 14.4.0. The match_request_uri option matches the request URI, since 18.23.0, 20.8.0 and 21.3.0. If you give several criteria, a request matches the endpoint when any one of them matches.
The example below uses a placeholder for Kamailio's address:
; <kamailio-ip> is the address of your Kamailio server
match = <kamailio-ip>
endpoint = kamailio-endpoint
The compiled note does not cover the section header or any type line, so copy those from the identify page before you use this block. The endpoint option names the endpoint that is identified.
The endpoint's context option sets the dialplan context for inbound sessions. Its default is default. Asterisk matches the endpoint first, and then the dialplan context runs the call.
Which pjsip.conf options matter behind Kamailio?
Several endpoint and transport options affect media and addresses when Kamailio sits in front. The table lists each one with its default where the compiled note gives it.
| Option | Section | Default | What the docs say |
|---|---|---|---|
context | endpoint | default | Dialplan context for inbound sessions |
direct_media | endpoint | yes | Whether media may flow directly between endpoints |
rtp_symmetric | endpoint | no | Enforces that RTP must be symmetric |
force_rport | endpoint | yes | Forces use of the return port |
rewrite_contact | endpoint | no | Changes the Contact or Record-Route header of inbound messages to the source IP address and port |
local_net | transport | Not in the note | Network to treat as local, for NAT purposes |
external_media_address | transport | Not in the note | External IP address for RTP handling |
external_signaling_address | transport | Not in the note | External address for SIP signaling |
The direct_media option decides whether media may flow directly between endpoints, and its default is yes. The rtp_symmetric option enforces symmetric RTP and defaults to no. The force_rport option forces use of the return port and defaults to yes.
The rewrite_contact option defaults to no. When it is on, it changes the Contact or Record-Route header of inbound messages to the source IP address and port.
The endpoint's transport option forces it to use a specific transport configuration to send SIP messages. For NAT, the transport options local_net, external_media_address and external_signaling_address come into play. The local_net option names the network to treat as local, in CIDR or dotted-decimal format.
What does call load distribution (algorithm 10) need?
Algorithm 10 is call load distribution, and it needs two things. Each destination needs a unique duid attribute, and ds_hash_size must be greater than 0. The docs say the hash size "must be greater than 0 to enable call load dispatching feature (alg 10)."
The docs' example line uses duid=abc in its attrs field. That value is only an example. Give each destination its own unique value.
More guides in Open-Source PBX and SIP Servers.
Related guides
Frequently asked questions
- Which dispatcher algorithm should I use?
- This note did not test the algorithms, so it cannot say which one fits your traffic. The docs list 0 (hash over Call-ID), 1 (hash over From URI), 2 (hash over To URI), 3 (hash over the Request-URI user), 4 (round-robin), 5 (hash over the authorization username) and 10 (call load distribution).
- Does the default kamailio.cfg include Asterisk support?
- No. The default kamailio.cfg has no Asterisk-specific define and does not load the dispatcher module, so dispatcher must be added by hand. The file also drops all SIP traffic until ACTIVE, WITH_AUTH or WITH_IPAUTH is defined.
Sources
- The Kamailio dispatcher module provides SIP load balancing and can act as a SIP traffic di (www.kamailio.org)
- allow_source_address([group_id]) equals allow_address(group_id, "$si", "$sp"); with no gro (www.kamailio.org)
- The default kamailio.cfg (Kamailio v6.2 on master) drops all SIP traffic until you define (github.com)
- The PJSIP endpoint context option sets the dialplan context for inbound sessions (default (docs.asterisk.org)
- In Asterisk, an identify section (res_pjsip_endpoint_identifier_ip) matches inbound reques (docs.asterisk.org)