Last active
December 21, 2023 12:01
-
-
Save rambabusaravanan/578df6d2486a32c3e7dc50a4201adca4 to your computer and use it in GitHub Desktop.
SPA - Apache, Nginx Configuration for Single Page Application like React.js on a custom path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To host on root path just use "<Location />" for http://mydomainname.in | |
# To host on non-root path use "<Location /myreactapp>" for http://mydomainname.in/mypath | |
# If non-root path, don't forgot to add "homepage": "/myreactapp" in your app's package.json | |
<VirtualHost *:80> | |
ServerName mydomainname.in | |
DirectoryIndex index.html | |
DocumentRoot /var/www/html | |
ErrorLog ${APACHE_LOG_DIR}/error.log | |
CustomLog ${APACHE_LOG_DIR}/access.log combined | |
<Location /myreactapp > | |
RewriteEngine on | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteRule . /myreactapp/index.html [L] | |
</Location> | |
</VirtualHost> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To host on root path just use "location /" for http://mydomainname.in | |
# To host on non-root path use "location /myreactapp" for http://mydomainname.in/mypath | |
# If non-root path, don't forgot to add "homepage": "/myreactapp" in your app's package.json | |
server { | |
server_name mydomainname.in; | |
index index.html; | |
error_log /var/log/nginx/error.log; | |
access_log /var/log/nginx/access.log; | |
root /usr/share/nginx/html; | |
location /myreactapp { | |
try_files $uri $uri/ /myreactapp/index.html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you [1] deploy in "/x" path and mention the same in [2] package.json as "homepage", then your react application will work properly
Both [1] and [2] are needed.
To achieve [1], try the below
You can access from http://localhost:xxxx/myreactapp where xxxx is the port docker exposes