Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] any suggestion for nginx configuration for prod or dev #116

Open
xBigDaddyx opened this issue Jan 12, 2025 · 12 comments
Open

[Question] any suggestion for nginx configuration for prod or dev #116

xBigDaddyx opened this issue Jan 12, 2025 · 12 comments
Labels
question Further information is requested

Comments

@xBigDaddyx
Copy link

xBigDaddyx commented Jan 12, 2025

Hello,
I’m looking for guidance on setting up Nginx for both production and development environments.

Minimal setup for rapid iteration and debugging
Handling frequent code changes without downtime
Simplified logging for development purposes
If there are specific examples, templates, or configurations you recommend, I’d greatly appreciate it. Additionally, if there are any caveats or common pitfalls to be aware of when setting up Nginx for these environments, please let me know.

Thank you for your help!

@shahghasiadil
Copy link
Contributor

@xBigDaddyx if you need use nginx with laravel octane I will share the configuration

@shahghasiadil
Copy link
Contributor

@xBigDaddyx there is one problem with this setup that is zero downtime we can resolve this by blue-green strategy with nginx or traefik load balancer or use Docker Swarm and Kubernetes Rolling updates

@shahghasiadil
Copy link
Contributor

shahghasiadil commented Jan 14, 2025

@xBigDaddyx I use this nginx configuration with laravel octane, reverb and frankenphp for production

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    server_tokens off;
    root /var/www/your-project/public;

    index index.php;
    charset utf-8;

    # Static file handling and fallback to Octane
    location / {
        try_files $uri $uri/ @octane;
    }

    location /index.php {
        try_files /not_exists @octane;
    }

    # Suppress logs for common non-essential requests
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Logging configuration
    access_log off;
    error_log /var/log/nginx/baheer-group-error.log error;

    # Custom 404 handling
    error_page 404 /index.php;

    # Laravel Octane fallback
    location @octane {
        set $suffix "";
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:8000$suffix;
    }

    # Laravel Reverb WebSocket Client (Laravel Echo)
    location ~ /app/(?<reverbkey>.*) {
        proxy_pass http://0.0.0.0:8080/app/$reverbkey;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    # Laravel Backend WebSocket Broadcasting
    location ~ ^/apps/(?<reverbid>[^/]+)/events$ {
        proxy_pass http://0.0.0.0:8080/apps/$reverbid/events;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

@smortexa smortexa added the question Further information is requested label Jan 19, 2025
@xBigDaddyx
Copy link
Author

@xBigDaddyx I use this nginx configuration with laravel octane, reverb and frankenphp for production

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    server_tokens off;
    root /var/www/your-project/public;

    index index.php;
    charset utf-8;

    # Static file handling and fallback to Octane
    location / {
        try_files $uri $uri/ @octane;
    }

    location /index.php {
        try_files /not_exists @octane;
    }

    # Suppress logs for common non-essential requests
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Logging configuration
    access_log off;
    error_log /var/log/nginx/baheer-group-error.log error;

    # Custom 404 handling
    error_page 404 /index.php;

    # Laravel Octane fallback
    location @octane {
        set $suffix "";
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:8000$suffix;
    }

    # Laravel Reverb WebSocket Client (Laravel Echo)
    location ~ /app/(?<reverbkey>.*) {
        proxy_pass http://0.0.0.0:8080/app/$reverbkey;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    # Laravel Backend WebSocket Broadcasting
    location ~ ^/apps/(?<reverbid>[^/]+)/events$ {
        proxy_pass http://0.0.0.0:8080/apps/$reverbid/events;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

@shahghasiadil thank you, i tried your config. now im facing always facing 502 when my app load a page of a filament widget.

@shahghasiadil
Copy link
Contributor

@xBigDaddyx can you share screen shot of nginx configuration?

@xBigDaddyx
Copy link
Author

@shahghasiadil same exactly like your config, just changed the root path. other than filament widget there is no problem, working smoothly. but when im going to page and there is filament widgets on that page, octane crashed so nginx thrown 502.

this is my command for running octane
'Octane' => 'php artisan octane:start --server=frankenphp --workers=4 --task-workers=6 --host=0.0.0.0 --port=8000 --admin-port=2019 --caddyfile=/home/xbigdaddyx/projects/teresa/Caddyfile',

@xBigDaddyx
Copy link
Author

Image

in first load its worked, but when tried the second octane crashed.

Image

@shahghasiadil
Copy link
Contributor

@xBigDaddyx did you try 0.0.0.0:8000 let me know if that works then the problem is in Nginx configuration, if 0.0.0.0:8000 had also same problem this means octane has an issue

@xBigDaddyx
Copy link
Author

@shahghasiadil yes i tried, its works.

@shahghasiadil
Copy link
Contributor

@xBigDaddyx please share nginx conf

@xBigDaddyx
Copy link
Author

xBigDaddyx commented Jan 24, 2025

@shahghasiadil

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;
    server_tokens off;
    root /home/xbigdaddyx/projects/teresa/public;

    index index.php;
    charset utf-8;

    # Static file handling and fallback to Octane
    location / {
        try_files $uri $uri/ @octane;
    }

    location /index.php {
        try_files /not_exists @octane;
    }

    # Suppress logs for common non-essential requests
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    # Logging configuration
    access_log off;
    error_log /var/log/nginx/teresa-error.log error;

    # Custom 404 handling
    error_page 404 /index.php;

    # Laravel Octane fallback
    location @octane {
        set $suffix "";
        if ($uri = /index.php) {
            set $suffix ?$query_string;
        }
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        proxy_pass http://127.0.0.1:8000$suffix;
    }

    # Laravel Reverb WebSocket Client (Laravel Echo)
    location ~ /app/(?<reverbkey>.*) {
        proxy_pass http://0.0.0.0:8080/app/$reverbkey;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Scheme $scheme;
        proxy_set_header SERVER_PORT $server_port;
        proxy_set_header REMOTE_ADDR $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }

    # Laravel Backend WebSocket Broadcasting
    location ~ ^/apps/(?<reverbid>[^/]+)/events$ {
        proxy_pass http://0.0.0.0:8080/apps/$reverbid/events;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

@itzJustKranker
Copy link

itzJustKranker commented Mar 30, 2025

is there any way to update the frankenphp caddy file to support the websockets proxy directly? if im building the image using WITH_REVERB=true and the octane server is hosted with frankenphp, then the frankenphp caddy file is used for the proxy yes?

so wouldnt there be some way to include the websocket proxy directly with the caddyfile? I'm not really versed with caddy, or even http server configs but maybe something like this

handle /app/* {
        reverse_proxy *:8080
    }

not even sure where that would go, maybe in this section?

{$CADDY_SERVER_SERVER_NAME} {

...

}

this is the one piece i can't seem to figure out, everything else seems to work fine, just can't get my app to connect to reverb at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants