I have a backend running at localhost:8080. And the path /runFuction executes a certain command.
Now I would like to use nginx as web server for my front end and only sent certain request to my backend.
My nginx config looks as follows
server {
listen 80;
server_name localhost;
location / {
root <path to site>;
index index.html index.htm;
}
location /api {
proxy_pass http://localhost:8080/;
}
}
In my index.html site I have a form that
...
<form action="api/runFuction" method="post" enctype="multipart/form-data">
....
<input type="submit" value="Do" />
</form>
I assumed that this would be possible but somehow I am missing something. How can I reach the backend path since I alway get a 404 page not there?
Could you try it?
location /api/ {
rewrite ^/api^/ /$1 break;
proxy_pass http://localhost:8080;
}
Related
I am having an issue with Nginx configuration. I have enabled proxy_intercept_errors and created rewrite rules to display a particular HTML page from the server directory in case if one of the 404, 502, 503 error occurs. I have tested these error codes and they are intercepted correctly and the necessary HTML page with text is displayed. But the issue is that these images somehow are not displayed in my custom HTML page. Maybe there is something wrong with file paths or Nginx configuration.
HTML file location on the server:
/var/www/html/
Images location:
/var/www/html/images/
For these aforementioned directories I have set chmod 755, but for files chmod 644
Nginx settings:
server {
listen 443 ssl http2;
server_name example.net;
proxy_intercept_errors on;
root /var/www/html;
error_page 404 #404;
error_page 502 #502;
error_page 503 #503;
access_log /var/log/nginx/example.net.ssl.access.log;
error_log /var/log/nginx/example.net.ssl.error.log;
ssl_certificate /etc/letsencrypt/live/example.net/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8080;
}
location #404 {
rewrite ^(.*)$ /404.html break;
}
location #502 {
rewrite ^(.*)$ /502.html break;
}
location #503 {
rewrite ^(.*)$ /503.html break;
}
HTML file content:
<!DOCTYPE html>
<meta charset="UTF-8">
<html>
<title>404</title>
<style>
img {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
<body>
<img src="/var/www/html/images/404.jpg" alt="404 Not Found" style="width:50%;">
</body>
</html>
When I change location in Nginx directly to the image files, then they are served and displayed correctly. Problem with images not displaying only appears when html file is served.
You need to tell Nginx how to handle the /images/404.jpg URL. It does not know that it came from a previous error_page 404 exception. The request to the URL that caused the error_page 404 exception and the request to the URL for the image are essentially independent of each other.
Assuming that localhost:8080 never needs to see any URL that begins with /images/, just add a simple location block:
location /images/ {}
It will use the value of root defined in the outer block.
If localhost:8080 needs to see some URLs that begin with /images/, use a more specific rule:
location = /images/404.jpg {}
location = /images/502.jpg {}
location = /images/503.jpg {}
I'm trying to set up an nginx server to serve a React app at the address http://mydomain/memorygame
Currently I have the following nginx routing config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/mydomain.com;
index index.html;
server_name mydomain.com www.mydomain.com;
location /memorygame {
root /var/www/mydomain.com/memorygame;
try_files $uri /$uri $uri/ /index.html $uri/index.html =404;
location ~* \.(css|js)$ {
try_files $uri /$uri =404;
}
}
The css files are stored under /var/www/mydomain.com/memorygame, in the index.html the link is /static/css/main.d5dd0bd5.css.
The index.html load fine, but the css requests aren't routed to where I want them. If I take out "/index.html" from try_files, the css loads, but index.html doesn't. How can I make them both work at the same time?
(My guess is that part of the problem is that css request issued goes to mydomain.com/static/css/style.css instead of mydomain.com/memorygame/static/css/style.css, but I might be wrong. If I'm correct, is there a way to auto-prepend the /static/css/style.css to be relative to the mydomain.com/memorygame folder?)
Thanks in advance!
I figured it out myself. Deleting everything out of the location /memorygame block did the trick. Nginx does what I was trying to achieve by default.
I have NGINX set up as a reverse proxy for a virtual network of docker containers running itself as a container. One of these containers serves an Angular 4 based SPA with client-side routing in HTML5 mode.
The application is mapped to location / on NGINX, so that http://server/ brings you to the SPA home screen.
server {
listen 80;
...
location / {
proxy_pass http://spa-server/;
}
location /other/ {
proxy_pass http://other/;
}
...
}
The Angular router changes the URL to http://server/home or other routes when navigating within the SPA.
However, when I try to access these URLs directly, a 404 is returned. This error originates from the spa-server, because it obviously does not have any content for these routes.
The examples I found for configuring NGINX to support this scenario always assume that the SPA's static content is served directly from NGINX and thus try_files is a viable option.
How is it possible to forward any unknown URLs to the SPA so that it can handle them itself?
The solution that works for me is to add the directives proxy_intercept_errors and error_page to the location / in NGINX:
server {
listen 80;
...
location / {
proxy_pass http://spa-server/;
proxy_intercept_errors on;
error_page 404 = /index.html;
}
location /other/ {
proxy_pass http://other/;
}
...
}
Now, NGINX will return the /index.html i.e. the SPA from the spa-server whenever an unknown URL is requested. Still, the URL is available to Angular and the router will immediately resolve it within the SPA.
Of course, now the SPA is responsible for handling "real" 404s. Fortunately, this is not a problem and a good practice within the SPA anyway.
UPDATE: Thanks to #dan
Using Nginx, I'm trying to configure my server to accept all domains that point to the IP of my server, by showing them a specific website, but when accessing the www.example.com (main website), I'd show an other content.
Here's what I did so far:
server {
// Redirect www to non-www
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
// rest of the configuration
}
server {
// Catch all
listen 80 default_server;
// I also tried
// server_name _;
// Without any luck.
// Rest of the configuration
}
The problem with this configuration is that every request made to this server not being www.example.com or example.com is took under example.com server configuration, not the catch all.
I'd like to cath only www.example.com/example.com in the first two configurations, and all the others in the last configuration.
I suggest putting your server on top of the file :)
I think nginx wants default servers to be on top of -a- file.
I have really much files on my server, but there is one with a default server as first server declaration, and that works.
I just installed nginx and php fastcgi about an hour ago, and after reading examples of a quick starting configuration, and the nginx documentation etc, I just cant get it to work.
No matter what I change or try, I always only get the "Welcome to Nginx!" screen on "localhost/..." - I cant even call a simple index.html
My config:
(the stuff in the comments is what I tried out)
// default nginx stuff (unchanged)
server {
#listen 80 default_server;
#listen 80 default;
listen 80;
#server_name localhost;
#server_name _;
#access_log /var/log/nginx/board.access_log;
#error_log /var/log/nginx/board.error_log;
#root /var/www/board;
#root /var/www/board/public/;
root /var/www/board/public;
#index index.html;
index index.html index.htm index.php;
}
If I understand it right, this should be the easiest setup, right? just define listen 80; and index index.html; but I just cant get it to work
The file /var/www/board/public/index.html exists and has content
Before I waste 2 more hours trying out something, can someone of you give it a quick watch and tell me what I'm doing wrong? Thanks.
Fundamentally you hadn't declare location which is what nginx uses to bind URL with resources.
server {
listen 80;
server_name localhost;
access_log logs/localhost.access.log main;
location / {
root /var/www/board/public;
index index.html index.htm index.php;
}
}