I am using nginx for reverse proxy. I am rewriting a directory using an alias. On my alias directory, I have Angular 2 build files which are using html5 routing. So when user will refresh the page it should rewrite to the index.html present in that directory only.
"control-pane is overwrite by an alias"
I already tried the "try_files $uri $uri/ /index.html =404;"
but this was referring to index.html inside main directory.
I also tried to tried to give an absolute path but still, it was not working.
server {
listen 80;
server_name xx.xxx.xx.xxx;
location / {
proxy_pass http://localhost:4000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /control-panel {
alias /var/www/html/soundoff-admin-dev/dist;
# here rewrite should override with index.html inside "dist" directory
}
}
Borrowed from https://stackoverflow.com/a/50863128/1264360
Try,
location /control-panel {
root /var/www/html/soundoff-admin-dev/dist;
try_files $uri $uri/ /control-panel/index.html;
}
Related
Those are the environment.
■Frontend
Server: Vercel
■Backend
Server: EC2
Middle: Nginx
note: wordpress is constructed in EC2
What I want is
① Accessing to https://example2.jp/hoge which is backend
② Redirecting to https://example.jp/hoge which is frontend to use Nginx showing wordpress page
I tried rewrite to achieve, but it couldn't work at all.
server {
listen 80;
server_name example2.jp;
root /var/www/example2/current/public;
location / {
proxy_pass http://example2;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
}
location /hoge/ {
root /var/www/html;
index index.php;
try_files $uri $uri/ /hoge/index.php?$args;
expires 7d;
rewrite ^ $scheme://example.jp/hoge/ permanent;
}
}
Could you give me tips?
I am trying to run two nodejs app one on default path (/) and another on /api , I have tried multiple ways but multiple locations are not working with nginx , only root domain (/) works fine, it doesn't matter which app I do assign (2nd or first). and if I try to visit (/api) it return 404 not found and (/) path is working fine.
Here is my default nginx file
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name example.com;
location /api {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
location / {
proxy_pass http://localhost:8081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
try_files $uri $uri/ =404;
}
}
What I am doing wrong. Any help will be apricated. thanks
After Doing a lot of googling and tried many ways I have found the solution and instead of using react app as dynamic loading i generated a build of react and stored that build on a particular location and served these build files on main location with nginx.
location / {
root /home/user/gui/build;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:3002/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /static/ {
root /home/user/gui;
try_files
/gui/build$uri
=404;
}
So here (/) will try files in the root folder which is here (/home/user/gui/build) and we have index.html which is generated by react build this index.html will be served. Now the next problem was to serve react generated static files so i have created a separate location (/static/) with the same logic of / path.
I'm having trouble setting up Nginx site configuration file.
So basically I'm serving 3 sites:
Dashboard /dashboard (react-based, operates at xxx/xxx/build/index.html)
API /api (node-based, operates using a proxy-pass and it works perfectly
Homepage / (static, operates at xxx/yyy/zzz/index.html)
My Nginx file looks as follows:
server {
listen 80;
server_name mydomain.com;
server_name xxx.xxx.xxx.xxx;
access_log /var/log/nginx/xxxxx;
error_log /var/log/nginx/xxxxx;
location /dashboard/ {
root /var/www/xxxxx/build;
index index.html;
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://localhost:5050/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
root /var/www/xxxxxx;
index index.html;
try_files $uri /$uri /index.html;
}
}
The problem lays mainly in the /dashboard path. For some reason it redirects to the homepage and never returns the actual dashboard.
I'm quite inexperienced in Nginx, so I'm not quite sure how to even debug it.
Just a guess.
root will add your location /dashbaord behind /var/www/xxxxx/build like so:
/var/www/xxxxx/build/dashboard
That location does not exists so it tries /index.php at the end of try_files which is an "absolute path" to your homepage?
I would try using alias instead of root (which will not add /dashboard to your root path) and/or try to fix your last parameter in try_files like so:
try_files $uri $uri/ ./index.html;
I am trying to serve a Node JS app alongside some static sites that are already functioning and only serving static content (/insta-app is one of the apps, there is another nginx file with another server block for the other ones which sit on different subdomains). I am successfuly getting the server.js app when I navigate to the URL /nodejsapp. The problem is that all the static content the app requires does not get served and comes up as a 404 (images, js files and css). I wrote a location block for nodejsapp/dist where the static content exists but this did not fix the problem. The content is being requested by a handlebars template that gets successfully called and is sitting in a views folder in the root. I am running the whole thing on an ubuntu server, and the node app is running through pm2 which is working fine when I request curl localhost:3000. How can I get the static content into the server?
Nginx server block:
server {
listen 443 ssl default_server;
root /var/www;
index index.html index.htm server.js;
server_name uat.com www.uat.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/uat.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/uat.com/privkey.pem;
location / {
try_files $uri $uri/ =404;
}
location ~ /.well-known{
allow all;
}
location /insta-app{
alias /var/www/insta-app/html;
allow all;
}
location /nodeJsApp{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $proxy_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# attempt to serve the static content of the app
location /nodeJsApp/dist{
root /var/www/sydney-sevens/dist;
try_files $uri $uri/ =404;
}
}
I'm using nginx in a reverse proxy configuration with apache2. I have used other, preconfigured web servers in this way and enjoyed complete control over redirects from an .htaccess file.
My current configuration does not allow for this. First, I'll explain what happens.
Let's say I want to redirect /google to http://google.com. I add the following line to my .htaccess file.
redirect 307 /google http://google.com
I know it's working because I can test with curl from my server. This is hitting apache directly, behind the proxy.
curl -I localhost:8080/google
and I get a 307 as I would expect.
But if this request hits nginx from the outside, nginx knows there is no such file in the web root and responds 404.
Is there a configuration change I can make to remedy this?
Here's my nginx configuration file for this vhost.
server {
listen 80;
root /var/www/mywebsite.com/html;
index index.php index.html index.htm;
server_name mywebsite.com;
access_log /var/log/nginx/mywebsite.com.access.log;
error_log /var/log/nginx/mywebsite.com.error.log;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\. {
deny all;
}
}
It seems that I might need to take the reverse approach and proxy everything but exclude certain file types from the proxy.
Thanks for advice folks!
If you want to forward that request to /google to apache, then just change the location ~ \.php$ pattern to include the request you desire:
location ~ (/google/?)|(\.php)$ {
On the other hand if you want nginx to handle the redirect you can add new rule:
location ~ /google {
return 307 http://google.com;
}
Just make sure to put it before the location block which contains try_files.
EDIT:
To forward all requests that don't hit a file use this:
location /
{
if (!-f $request_filename) #test if a static file does not exists
{
# forward the request if there is no file to serve:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}
Additionally if you want for nginx to also serve entire directories then add another condition:
!-d $request_filename
Also note that if you want only specific files to be served then instead location / use the pattern to match those files. For example if you want to serve only jpg's and css's use
location ~ \.(jpg|css)$
EDIT2: Here you have a simplified version of your script, which is also more robust - lets you serve only the types of files you want:
server {
listen 80;
server_name test.lcl;
root /home/www/test;
location / {
try_files $uri $uri/ #apache;
}
location #apache
{
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
}