I have problem with proxy_pass in nginx. I have one page, for example test.com. In test.com/article I use proxy_pass:
server {
listen 80;
server_name test.pl;
root /var/www/html/test.com;
index index.html;
try_files $uri /index.html;
location /article/ {
proxy_pass https://anotherpage.com/;
}
}
And it's work, but if I have links in page test.com/article, they look like: test.com/some-link
I want it would be: test.com/article/some-link
Any ideas or tips how can I do that?
Related
I have two apps build in react using vite and I want to combine those two apps in one on the same port:
For example in nginx I want something like this:
http://localhost -> redirect to http://localhost:3000;
http://localhost/path-to-app2 -> redirect to http://localhost:4000;
What I tried with the apps in development mode:
server {
listen 80;
location / {
proxy_pass http://localhost:3000;
}
location /path-to-app2 {
proxy_pass http://localhost:4000;
}
}
also I tried with builded apps:
server {
listen 80;
location / {
root /usr/share/nginx/html/app1;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
location /path-to-app2 {
root /usr/share/nginx/html/app2;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
The problem is that all routes are redirected to location /. I also tried to change location / to location = / but also didn't work...
I also tried to app2 who has the path /path-to-app2 to add in package.json: "homepage": "/path-to-app2" but also didn't work. I don't succed to make the path / exact
I someone here who faced the same issue like me?
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I am trying to implement something like that in the nginx conf:
subdomain
sub.domain.com -> Serve html
sub.domain.com/api -> proxy to port 3001
sub.domain.com/viewer -> serve another html
subdomain2
sub2.domain.com -> proxy to port 3000
The only route that doesn't work is the viewer, I get the html from the "location /". All other configurations work well.
I tried to move the viewer to the bottom then to the top and to the middle no matter what it doesn't work.
I use CentOS7. This is the configurations currently in the server:
events {
}
http {
server {
listen 80;
listen [::]:80;
server_name www.sub.domain.com subdomain.com;
location /viewer {
root /opt/viewer/;
try_files $uri /index.html;
index index.html;
}
location / {
root /opt/client-bo/;
try_files $uri /index.html;
index index.html;
}
location /api {
proxy_pass "http://localhost:3001";
}
}
server {
listen 80;
server_name www.sub2.domain.com sub2.domain.com;
listen [::]:80;
location / {
proxy_pass "http://localhost:3000";
}
}
}
Thanks!
If your viewer app located in the /opt/viewer directory and you want it to be available under the /viewer URI prefix, you should use root /opt; for the location /viewer { ... }. Check the difference between root and alias directives.
Next, the very last argument of the try_files directive is treated as the new URI to re-evaluate, so you /index.html being treated as the new URI going to be served with the location / { ... }. You should change that directive to
try_files $uri /viewer/index.html;
I have the following server in NGINX and it works fine. But, I am wondering is it possible to add text to a response from a remote URL where hosts my before_body.txt and after_body.txt? Is there any way to tackle this?
server {
listen 80;
root /storage/path;
index index.html;
server_name test.domain.com;
location / {
try_files $uri $uri/ =404;
add_before_body /src/before_body.txt;
add_after_body /src/after_body.txt;
autoindex on;
}
location /src/ {
alias /storage/path/content/;
}
}
I have resolved with replacing the alias as follows:
location /src/ {
proxy_pass https://externalserver.com/;
}
I'm starting to go nuts at this. For some reason, routing wont work on my single page application. So www.example.com works, but not www.example.com/service. I read a lot of posts on how to fix it, but nothing seems to work.
This is my config file at /etc/nginx/conf.d/App.conf
server {
listen 80;
server_name example.com *.example.com;
return 301 https://example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /path/to/certificate/;
ssl_certificate_key /path/to/certificate/key;
root /var/www/App/public;
index index.html;
location / {
try_files $uri /index.html;
}
ssl_session_timeout 5;
}
I have tried all kind of different "location" routes, and nothing seems to work. I do also restart the service with "sudo service nginx restart" everytime I change.
Any clues?
In the comments you said there's a small fixed set of possible routes. In that case you can add a location block for each route, with an alias to the top-level, for example,
location / {
try_files $uri $uri/ =404;
}
location /services {
alias /var/www/App/public;
try_files $uri $uri/ =404;
}
Edit: Or, if you want to serve the top-level index.html in response to any request at all,
location / {
try_files /index.html =404;
}