> ## Documentation Index
> Fetch the complete documentation index at: https://docs.pgw.sipmicro.services/llms.txt
> Use this file to discover all available pages before exploring further.

# Configure Asterisk for pgw

> Change the PJSIP settings of each user, so that Asterisk sends that user's calls through pgw.

When you finish this page, Asterisk accepts registrations that arrive through pgw and sends the calls for those devices back to pgw. Asterisk needs no module and no patch. Only the users whose apps register through pgw need the changes.

The key setting is `support_path`. pgw adds a Path header to every REGISTER that it forwards, which asks the registrar to send that device's calls through pgw. Asterisk honors the header only when `support_path` is on.

## Before you begin

* Asterisk with the PJSIP channel driver. The examples use user `1001`.
* An upstream in pgw whose host is this Asterisk server. The [quickstart](https://docs.pgw.sipmicro.services/quickstart.md) creates one.
* The addresses that Asterisk sends calls from, listed in the upstream's `invite_source_cidrs`. See [Allow the PBX to send calls to pgw](https://docs.pgw.sipmicro.services/security.md#allow-the-pbx-to-send-calls-to-pgw).

## Configure a user

**1. Set up the AOR**

In `/etc/asterisk/pjsip.conf`, add these settings to the user's AOR section:

```ini theme={null}
[1001]
type=aor
support_path=yes
max_contacts=11
remove_existing=no
maximum_expiration=3600
```

| Setting | Why |
| - | - |
| `support_path=yes` | Asterisk stores pgw's Path with the contact and sends the user's calls to pgw. |
| `max_contacts=11` | Each device is one contact, and pgw allows 10 devices for one account by default. pgw also needs one contact free. See [Leave room for one more contact](#leave-room-for-one-more-contact). |
| `remove_existing=no` | A new device doesn't push another device's contact out. |
| `maximum_expiration=3600` | pgw asks for one-hour registrations. A lower limit makes pgw wake each app more often to register again. |

**2. Set up the endpoint**

In the user's endpoint section, turn off contact rewriting:

```ini theme={null}
rewrite_contact=no
```

With `rewrite_contact=yes`, Asterisk replaces the app's contact with the address that the REGISTER came from, which is pgw. Calls to the app then fail.

If the phones are behind NAT, which mobile phones usually are, also set these, so that audio reaches them. pgw doesn't relay media.

```ini theme={null}
rtp_symmetric=yes
force_rport=yes
direct_media=no
```

**3. Reload PJSIP**

```bash theme={null}
asterisk -rx "module reload res_pjsip.so"
```

## Check that it works

Register the app through pgw, then list pgw's bindings:

```bash theme={null}
pgw binding list
```

A binding for `1001` means that Asterisk accepted the registration and kept pgw's Path. By default (`strict_path`), pgw refuses a registration when the PBX's answer drops the Path. Without that check, a device would look registered while its calls bypassed pgw.

Then close the app and call `1001` from another phone. The app rings.

## If it doesn't work

| Symptom | Fix |
| - | - |
| REGISTER gets `502 Bad Gateway` with `Warning: 399 pgw "upstream did not keep the Path"` | Set `support_path=yes` on the AOR, and reload PJSIP. |
| The app registers, but calls never reach pgw | Set `support_path=yes` on the AOR and `rewrite_contact=no` on the endpoint. |
| A call gets `404 Not Found` from pgw, and pgw logs `invite refused` with the reason `no flow token in the request` | Set `support_path=yes` on the AOR. |
| REGISTER gets `403` from Asterisk | The AOR reached `max_contacts`. Raise it. |
| REGISTER gets `200` with `Expires: 0`, and pgw logs `upstream granted no registration` | Check `minimum_expiration` and `max_contacts` on the AOR. |
| The call connects with no audio | Set `rtp_symmetric=yes`, `force_rport=yes`, and `direct_media=no` on the endpoint. |

For other symptoms, see [Troubleshooting](https://docs.pgw.sipmicro.services/troubleshooting.md).

## Leave room for one more contact

Asterisk refuses a registration that would take an AOR over `max_contacts`, and it answers with `403`. pgw can't tell that `403` from a refused password, so a full AOR looks like a credentials problem. Avoid it by counting what registers on the AOR:

* One contact for each device that the user can have. pgw allows `max_bindings_per_aor`, 10 by default.
* One more, which pgw needs free.
* One for each phone that registers without pgw, such as a desk phone.

In managed mode, pgw by default registers one contact for the user instead of one for each device, so the AOR needs far fewer contacts. Keep `max_contacts` at the count above anyway, because during an upgrade or a rollback that changes how pgw registers, the AOR briefly holds both kinds of contact.

Don't solve this with `remove_existing=yes`. Asterisk then makes room by removing an existing contact, and if that contact belongs to a desk phone, the phone gets no calls until it registers again.

## Special cases

### Asterisk without Path support

If your Asterisk can't use Path, create the upstream with the `masquerade` contact strategy and leave out `support_path`. pgw then registers its own address as each device's contact. The other settings on this page still apply.

### One Asterisk for several SIP domains

On a managed upstream with [several domains](https://docs.pgw.sipmicro.services/upstreams/several-domains.md), pgw registers each user at the user's domain, such as `sip:1001@acme.example.com`. Asterisk looks for an AOR named `1001@acme.example.com` first, and then for `1001`. To keep `1001` at one domain apart from `1001` at another, name each AOR and endpoint with its domain.

### The realm in managed mode

Asterisk challenges with the realm `asterisk` by default, but in managed mode pgw challenges the apps itself. If your apps were set up with the realm `asterisk`, create the managed upstream with `--realm asterisk`, so that their saved credentials still match. You can't change the realm later.

## Next steps

> [card] [Configure apps for pgw](https://docs.pgw.sipmicro.services/apps/overview.md)
>
> Register your app's push credentials, so that pgw can wake the app.

> [card] [Managed mode](https://docs.pgw.sipmicro.services/upstreams/managed-mode.md)
>
> Let pgw keep registrations alive when Asterisk grants only short ones.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.