yaswanthgupta wrote a reply+100 XP
3mos ago
I have revised my setup. I am hosting the site in nginx using port 8001.
Here are my files. I will describe the issue at the end of the files.
nginx.conf file
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# =========================================================
# 1. MAIN GATEWAY (Entry Point)
# =========================================================
server {
listen 8000;
server_name localhost;
# Forward /APGBDOS traffic to Port 8001
location /APGBDOS/ {
proxy_pass http://127.0.0.1:8001/;
proxy_set_header Host $host:8000;
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;
# Important: Tell Laravel about the prefix
proxy_set_header X-Forwarded-Prefix /APGBDOS;
# Rewrite location headers in redirects
proxy_redirect http://127.0.0.1:8001/ http://$host:8000/APGBDOS/;
proxy_redirect / /APGBDOS/;
}
}
# =========================================================
# 2. INTERNAL APP: APGBDOS (Runs on Port 8001)
# =========================================================
server {
listen 8001;
server_name localhost;
root D:/apps/APGBDOS/public;
index index.php index.html;
charset utf-8;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
.env file
APP_URL=http://server_ip:8000/APGBDOS
ASSET_URL=http://server_ip:8000/APGBDOS
vite.config.ts file
import { wayfinder } from '@laravel/vite-plugin-wayfinder';
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import laravel from 'laravel-vite-plugin';
import { defineConfig } from 'vite';
export default defineConfig({
base: '/APGBDOS/build/',
plugins: [
laravel({
input: ['resources/css/app.css', 'resources/js/app.tsx'],
ssr: 'resources/js/ssr.tsx',
refresh: true,
}),
react({
babel: {
plugins: ['babel-plugin-react-compiler'],
},
}),
tailwindcss(),
wayfinder({
formVariants: true,
}),
],
build: {
sourcemap: false,
rollupOptions: {
output: {
manualChunks: undefined,
},
},
},
esbuild: {
jsx: 'automatic',
},
server: {
host: '0.0.0.0',
port: 5173,
strictPort: true,
hmr: {
host: 'server_ip',
protocol: 'ws',
},
fs: {
strict: false,
},
watch: {
usePolling: true,
},
},
optimizeDeps: {
include: ['lucide-react', 'react', 'react-dom'],
},
});
AppServiceProvider.php file boot method
public function boot(): void
{
// If APP_URL is set in .env, force Laravel to use it as the root
if (config('app.url')) {
URL::forceRootUrl(config('app.url'));
}
// Handle X-Forwarded-Prefix header from nginx
if ($prefix = request()->header('X-Forwarded-Prefix')) {
// This ensures Laravel knows about the prefix
request()->server->set('SCRIPT_NAME', $prefix . '/index.php');
}
part of login.tsx file
import login from "@/routes/login";
post(login.store.url(), {
onFinish: () => reset("password")
});
Issue:
As my AppServiceProvider.php file has URL::forceRootUrl(config('app.url'));, wayfinder is generating absolute urls instead of relative urls. Because of this, I get this error in console while submitting the login form (observe there is double http).
POST http://http//server_ip:8000/APGBDOS/login net::ERR_NAME_NOT_RESOLVED
If I remove URL::forceRootUrl(config('app.url')); in AppServiceProvider.php file making the wayfinder to generate relative paths. I get this error
404 Not Found nginx/1.28.0
For Reference:
part of resources\js\routes\login\index.ts file BEFORE adding URL::forceRootUrl(config('app.url')); in AppServiceProvider.php file
store.definition = {
methods: ["post"],
url: '/login',
} satisfies RouteDefinition<["post"]>
/**
* @see \Laravel\Fortify\Http\Controllers\AuthenticatedSessionController::store
* @see vendor/laravel/fortify/src/Http/Controllers/AuthenticatedSessionController.php:58
* @route '/login'
*/
store.url = (options?: RouteQueryOptions) => {
return store.definition.url + queryParams(options)
}
part of resources\js\routes\login\index.ts file AFTER adding URL::forceRootUrl(config('app.url')); in AppServiceProvider.php file
store.definition = {
methods: ["post"],
url: '//http://server_ip:8000/APGBDOS/login',
} satisfies RouteDefinition<["post"]>
/**
* @see \Laravel\Fortify\Http\Controllers\AuthenticatedSessionController::store
* @see vendor/laravel/fortify/src/Http/Controllers/AuthenticatedSessionController.php:58
* @route '//http://server_ip:8000/APGBDOS/login'
*/
store.url = (options?: RouteQueryOptions) => {
return store.definition.url + queryParams(options)
}
yaswanthgupta started a new conversation+100 XP
4mos ago
My Setup
- Laravel React Starter Kit hosted on IIS (Port 81)
- Nginx running on Port 8000
Configuration Details
Nginx
-
A
locationblock is configured withproxy_passpointing to port 81 -
When a user visits,
http://server_ip:8000/app1it proxies tohttp://server_ip:81/app1
Vite - vite.config.js
base: '/app1/'
Laravel - bootstrap/app.php
->withMiddleware(function (Middleware $middleware) {
$middleware->trustProxies(at: '*');
Laravel - .env
APP_URL=http://server_ip:8000/app1
ASSET_URL=http://server_ip:8000/app1
Issue
I am able to access the site via http://server_ip:8000/app1 but the URL automatically goes back to http://server_ip:8000/ . Now I cannot login not i cannot just refresh the browser because the app is in http://server_ip:8000/app1
I have also tried in multiple ways like proxying through IIS also (without Nginx). Here also, I face the same issue.
Kindly help me out !