I am looking to convert nginx config file to caddy with multiple sub path api configs.
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
root /usr/local/var/www/example/ui;
index index.html index.htm;
location /api/ {
proxy_pass http://localhost:9000/api/;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
}
location /usermgmt/ {
proxy_pass http://127.0.0.1:9000/;
proxy_redirect off;
proxy_buffering off;
}
location /integrations/ {
proxy_pass http://127.0.0.1:9003/;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
}
I tried with caddy reverse_proxy but not working. Api request should go example.com/api instead of example.com/integration/api through proxy.
Please help me how to solve this problem.
You can do this using the handle directive.
example.com {
handle /api/ {
reverse_proxy localhost:9000
}
handle /usermgmt/ {
reverse_proxy localhost:9003
}
root /usr/local/var/www/
handle {
file_server
}
}
This works because handle is mutually exclusive. Documentation here:
https://caddyserver.com/docs/caddyfile/directives/handle
Related
I have setup nginx with wordpress and it is working fine. now i have created a react application, which is running in port 3000. i want my nginx server to pass the request to react server if certain location match.
below is the nginx configuration with wordpress and react app.
listen 80;
server_name aaroogya.org;
return 301 https://aaroogya.org$request_uri;
}
server {
# listen 80;
root /var/www/wordpress;
index index.php index.html index.htm index.nginx-debian.html;
server_name aaroogya.org www.aaroogya.org;
#location = /favicon.ico { log_not_found off; access_log off; }
#location = /robots.txt { log_not_found off; access_log off; allow all; }
#server_name testbed2.covidhelp.in;
location /covidhelp{
#root /var/www/;
# index index.html;
add_header Access-Control-Allow-Origin http://127.0.0.1:3000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/ ;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/aaroogya.org/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/aaroogya.org/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
when i visit https://www.aaroogya.org/covidhelp/
it redirect the request to react server but when i tried to load all the static file like bundle.js then it's not working.
for e.g
https://www.aaroogya.org/covidhelp/static/js/main.chunk.js -- not working example
https://www.aaroogya.org/covidhelp/static/js/main.chunk.js/ -- added a trailing slash and its working fine.
I've resolved the issue with 2 steps.
Check /var/log/nginx/error.log
connect() failed(111: Connection refused) while connecting to upstream, client: * .*.*.*, server: * .*.*.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.*.*.*"
Upstream was still 127.0.0.1:8000 even if I set upstream to 127.0.0.1:3000 in nginx conf file.
Replace server 127.0.0.1:8000 with server 127.0.0.1:3000 in /etc/nginx/conf.d/virtual.conf and restart nginx.
Below:
server {
listen 80;
server_name SERVER_IP_ADDRESS;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
Then:
sudo /etc/init.d/nginx restart
Finally, it works with no 502 error.
I've hosted my first Vue and Node app but I have a problem. I want to load Vue files on diferent port so there is less stress on node. The problem is that with this current configuration I get this in browser: Cannot GET / even though when in Node router I add route with url / I get something. But I need to load this url from vue router not from express router. Why it loads from express ? This is my configuration file nginx:
server {
listen 80;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com;
# Use the Letā€™s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location /api {
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://localhost:5000;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
location / {
root /var/www/html/Web/dist; // Vue dist folder
}
}
In your nginx config you need to add try_files $uri $uri/ /index.html; to you / location like so. This sends everything to your index.html file.
location / {
root /var/www/html/Web/dist; // Vue dist folder
try_files $uri $uri/ /index.html;
}
I am having trouble accessing my node server externally. Internally, I can access it fine, but I am unable to do so otherwise.
Here is my nginx configuration. I simply want to access my website using only my external IP (for example, 133.21.29.21)
server {
listen 80 default_server;
listen [::]:80 default_server;
listen 2500;
location / {
proxy_pass http://127.0.0.1:3005;
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;
}
}
server code
app.server.listen(3003, "0.0.0.0", () => {
console.log(app.server.address());
});
I am running on port 3003 at the moment. I have tried sever nginx configurations and changing my server code as well (changing port, omitting "0.0.0.0", using "127.0.0.1") but I have not had any luck.
I've been trying to access my server by going to my-external-ip:2500, but i've tried accessing through other ports as well.
I've disable the ufw firewall and still have not had any luck. Curling locally works fine.
What am I doing incorrectly?
I think you are missing a proxy redirect
Take a look at the following example NGINX configuration file, the location / { } is pointing to a Node server on port 9080 and it works by navigating to https:// ... .com
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
ssl on;
ssl_certificate /etc/letsencrypt/live/thedomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/thedomain.com/privkey.pem;
access_log /var/log/nginx/thedomain.access.log;
error_log /var/log/nginx/thedomain.error.log;
server_name _;
root /var/www/html;
index index.html;
gzip on;
gzip_proxied any;
gzip_types text/css text/javascript text/xml text/plain application/javascript application/x-javascript application/json;
location /.well-known/ {
try_files $uri $uri/ =404;
}
location /jenkins {
include /etc/nginx/proxy_params;
proxy_pass http://localhost:8080;
proxy_read_timeout 90s;
proxy_redirect http://localhost:8080 https://www.thedomain.com/jenkins;
}
location /wss/pforex {
include /etc/nginx/proxy_params;
proxy_pass http://localhost:9190;
proxy_http_version 1.1;
proxy_read_timeout 90s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect http://localhost:9190 https://www.thedomain.com/wss/pforex;
}
location / {
include /etc/nginx/proxy_params;
proxy_pass http://localhost:9080;
proxy_read_timeout 90s;
proxy_redirect http://localhost:9080 https://www.thedomain.com;
}
}
Nginx works as a front-end server, which in this case proxies the requests to a node.js server. Therefore you need to set up a Nginx config file for the node.
Create the file yourdomain.com at /etc/nginx/sites-available/:
# the IP(s) on which your node server is running. I chose port 3003.
upstream app_yourdomain {
server 127.0.0.1:3003; # can use localhost as well
keepalive 8;
}
# the Nginx server instance
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
access_log /var/log/nginx/yourdomain.com.log;
# pass the request to the node.js server with the correct headers
# and much more can be added, see Nginx config options
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://app_yourdomain/;
proxy_redirect off;
}
}
Besides you can even omit giving ip to listen method and it will take localhost by default
const app = express();
app.listen(3003, () => {
console.log(app.server.address());
});
If you are accessing the server directly with IP then you need to change
server_name yourdomain.com www.yourdomain.com;
with
server_name _;
I have three files on my nginx conf.d -
example.com
www.example.com
other.example.com
example.com is my preferred domain and is proxy passing a nodejs app.
example.com has the following config
server {
listen 80;
server_name example.com;
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;
}
}
www.example.com has the following config:
server{
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
In other.example.com I have the following config
server {
listen 80;
server_name other.example.com;
proxy_redirect off;
root /opt/other;
index index.html;
}
But when I go to other.example.com I get the same result as www.example.com and example.com
Any thoughts how to fix this behaviour?
Problem Solved.
I forgot to name other.example.com file with the .conf extension.
thanks to #Curious
I'm trying to configure nginx to:
http://www.domain.tld --> https://domain.tld
http://domain.tld --> https://domain.tld
http://api.domain.tld --> https://api.domain.tld
The 'www'-webroot serves static HTML (AngularJS) and the API serves an Node.JS app that should 'upstream' from localhost:3000. I guess I'm in the right direction, however it doesn't seem to work for me. Here's what I've got so far:
upstream api_server {
server localhost:3000;
keepalive 64;
}
server {
listen 80;
server_name api.domain.tld;
return 301 https://api.domain.tld$request_uri;
}
server {
listen 80;
server_name *.domain.tld www.domain.tld;
return 301 https://domain.tld$request_uri;
}
server {
listen 443 ssl;
server_name api.domain.tld;
ssl_certificate /etc/ssl/ssl_cert.crt;
ssl_certificate_key /etc/ssl/ssl_key.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://api_server/;
proxy_redirect off;
}
server {
listen 443 ssl;
server_name *.domain.tld www.domain.tld;
ssl_certificate /etc/ssl/ssl_cert.crt;
ssl_certificate_key /etc/ssl/ssl_key.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/webroot/;
}
Right now this is all in my /etc/nginx/conf.d/domain.tld.conf file.
Any help would be really appreciated.
EDIT:
I've figured it out myself (a bit of help from Tan Hong Tat), so the example is updated.
If you've got any improvements please do tell, I'll update it.
Redirect HTTP to HTTPS in the server block for HTTP. Remove the listen 80 in the HTTPS server block.
server {
listen 80;
server_name domain.tld www.domain.tld;
return 301 https://domain.tld$request_uri;
}
server {
listen 80;
server_name api.domain.tld;
return 301 https://api.domain.tld$request_uri;
}
server {
listen 443 ssl;
server_name domain.tld www.domain.tld api.domain.tld;
location / {
proxy_redirect off;
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_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_cache one;
proxy_cache_key sfs$request_uri$scheme;
proxy_pass http://domain_tld_api_server;
}
}