I'm creating a website with NGINX handling Static content, SSL and all that stuff, while my API and non-static websites are handled by Express.
Now, I'd like NGINX to pass stuff like "/update" to Express. However, I'm not sure how to configure that.
Is the example below from DigitalOcean functional for https websites in the first place? Shouldn't I configure the same SSL certificate that NGINX uses to Express, so it redirect to https://website.com/update instead of http://website.com/update?
location / {
proxy_pass http://localhost:8080;
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;
}
Thanks in advance!
To proxy pass any API request starting with /update Example: http://localhost:3000/update, http://localhost:3000/update/test etc.. You can use below nginx config inside server block:
location /update {
proxy_pass http://localhost:3000;
}
If you want to redirect http://website.com/update to https://website.com/update . You will need to create a server at 80 port which will redirect any request that come at 80 port will be redirect to https://website.com/update
server {
listen 80;
listen [::]:80;
server_name website.com;
return 301 https://website.com$request_uri;
}
Related
can you help on the nginx configration .
my application on node express js I have two application express js i want to run in single domain.
This my server like : app.example.com
app1 run :- app.example.com/allinone/
app2 run :-app.example.com/app/
I am using the express redirect based on the root URL redirect.
res.redirect('/login')
this response redirects to the root server domain URL I want that to redirect to the location URL.
here my nginx server block code
server {
listen [::]:80;
listen 80;
server_name app.example.com;
location /allinone/ {
proxy_set_header Host $host;
proxy_redirect ~/(.*)$ /allinone/$1;
proxy_pass http://127.0.0.1:5002;
}
location /app/ {
proxy_set_header Host $host;
proxy_redirect ~/(.*)$ /app/$1;
proxy_pass http://127.0.0.1:5000;
}
}
app is working with the location host when i move to production with sub url it was not working.
I have also tried those solutions.
Express.js redirect with virtual path
proxy_set_header Host $host;
#replase with
proxy_set_header Host $http_host;
Express.js redirect with virtual path
Nginx is redirecting proxy_pass to root path automatically
Error
This page isn’t working app.example.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
I have tried a new clean browser but still have the same Error.
but direct open api GET endpoint /allinone/openapi not working.
I am having an issue when I GET request data from my react.js app to my Express.js backend, I am getting HTML garbage back instead of what my backend is supposed to return. Upon debugging and researching, I found out that the reason for that is because my GET route "/order/getTimeSlots" actually returns HTML content that is displayed when I go to mywebsite.com/order/getTimeSlots instead of my backend. I have set up proxy in my development environment and it works, however, it does not work in production. I am using nginx to serve my react app and here is my nginx config
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/mywebsite.com;
index index.html index.htm index.nginx-debian.html;
server_name mywebsite.com www.mywebsite.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# try_files $uri $uri/ =404;
try_files $uri /index.html;
}
location /ordersubmit {
proxy_pass http://localhost:8080; #this is where my backend app is running on
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 /order/getTimeSlots {
proxy_pass https://localhost:8080; #this is where my backend app is running on
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;
}
}
When trying curl on localhost:8080/getTimeSlots from my server console, I am getting the correct response.
I know there is a problem with my reverse proxy set up, but I cannot figure out what the issue is, so I was wondering if someone here can help
Thank you everyone
Fixed the issue myself, I realized that I was adding those locations under the server that listens to port 80, but since I am using https, I was supposed to use port 443
I have a server in windows using Plesk.
I have a domain (example.com), also I bought a SSL certificate for this domain.
I just installed successfully and configure the domain and ssl in my server. So now I can join to my web using https://www.example.com or example.com and will be redirect to https://....
Until that everything works fine.
But now I have been developed an API in GoLang which can’t start to listen at 443 port for some reason. (I thought that maybe because is being already used ?) So I changed to 8081 port. Now when I want to make a request to my API I have to use https://www.example.com:8081/api/v1/users for example.
The problem is that some applications show me a error “Certificate invalid” which I think is because the port is not 443. Is there any way that I can run go in 443?
The code in GO is this: (The crt and key are the ones provided by GoDaddy, is where I bought the SSL)
func main() {
router := NewRouter()
handler := cors.AllowAll().Handler(router)
log.Fatal(http.ListenAndServeTLS(":8081", "tls.crt", "tls.key", handler))
}
Run the whole Golang application behind nginx (reverse proxy):
Create a Virtual Host Server Block in Nginx using your domain.
https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04
Setup your SSL certs
Point that domain to your Golang App
server {
server_name example.com;
location / {
proxy_pass http://localhost:8081;
proxy_http_version 1.1;
proxy_set_header Host $host;
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;
proxy_cache_bypass $http_upgrade;
proxy_pass_request_headers on;
proxy_read_timeout 150;
}
ssl_certificate /path/to/chainfile/example.com/abcd.pem;
ssl_certificate_key /path/to/privatekeyfile/example.com/abcd.pem;
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
I'm having problems serving favicons with nginx as a reverse proxy in front of my express app.
Tried to search for answers but couldn't find any. My configuration file is as shown:
server {
listen 80;
server_name vogueverve.com;
location / {
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;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|w$
root /var/www/hashiontag/public;
}
}
Please help! Thank you so much!
I found my answer here:
https://serverfault.com/questions/308299/how-to-set-a-favicon-ico-for-a-specific-virtual-host-on-nginx#answer-308304
Apparently, for nginx, the default is to put the favicon at the root directory, because nginx directs the clients to get favicon from www.domainname.com/favicon.ico by default.
This means that, (I think) with nginx as the reverse proxy, the favicon request never reaches the express layer, and hence it can't serve it.
My goal is to redirect from port 80 to 443 (force https), but can't manage to get a working https configuration first. I get a 503 Server Error and nothing appears in the logs. I've looked at all the posts on SO and SF, none of them worked (X_FORWARDED_PROTO, X-Forwarded-For headers don't make a difference.). I'm on EC2 behind a load balancer, and so I don't need to use the SSL-related directives as I've configured my certificate on the ELB already. I'm using Tornado for a web server.
Here's the config, if anyone has ideas, thank you!
http {
# Tornado server
upstream frontends {
server 127.0.0.1:8002;
}
server {
listen 443;
client_max_body_size 50M;
root <redacted>/static;
location ^~/static/ {
root <redacted>/current;
if ($query_string) {
expires max;
}
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends;
}
}
}
Well, there are two different tasks:
If you need to redirect all your http traffic to https, you'll need to create http server in nginx:
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
Second note, if your SSL is terminated at ELB than you dont need ssl enabled nginx server at all. Simply pass traffic from ELB to your server 80 port.