I want to pass requests like this:
http://mydomain.io --> http://127.0.0.1:8080
http://mydomain.io/aa/bb --> http://127.0.0.1:8080/aa/bb
http://api.mydomain.io --> http://127.0.0.1:10000
http://api.mydomain.io/cc/dd --> http://127.0.0.1:10000/cc/dd
and my /etc/nginx/conf.d/default.conf file:
listen 80;
server_name mydomain.io;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
server {
listen 80;
server_name api.mydomain.io;
location ~ ^/(.*)$ {
proxy_pass http://127.0.0.1:10000/$1;
}
}
When I send request to http://api.mydomain.io:10000/xxx, it works,
but http://api.mydomain.io/xxx doesn't work.
(It returns 502 Bad Gateway Error)
What is the problem with my config file?
default.conf
server { # This one before the wilcard domain
listen 80;
server_name api.mydomain.io;
location / {
proxy_pass http://127.0.0.1:10000;
}
}
server { # always at the end
listen 80 default_server;
server_name *.mydomain.io;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
Related
I have setup a docker environment for testing nginx reverse proxy.
Instead of setting up a subdomain for every backend, i would like to have 1 subdomain with a location pointing to the individual backend:
while this works as expected:
server {
listen 80;
server_name dashboard.test.lan;
location / {
proxy_pass http://172.17.0.1:82;
}
}
server {
listen 80;
server_name gitlab.test.lan;
location / {
proxy_pass http://172.17.0.1:83;
}
}
server {
listen 80;
server_name portainer.test.lan;
location / {
proxy_pass https://172.17.0.1:9443;
}
}
...this does not work:
server {
listen 80;
server_name proxy.test.lan;
location /dashboard {
proxy_pass http://172.17.0.1:82;
}
location /gitlab {
proxy_pass http://172.17.0.1:83;
}
location /portainer {
proxy_pass https://172.17.0.1:9443;
}
}
Maybe you guys can give me a hint or explanation why this does not work.
I have two websites one is server1.example.com the other is server2.example.com. So I add two servers in nginx_config
server{
listen 80;
server_name server1.example.com;
location / {
proxy_pass http://127.0.0.1:9090;
}
}
server{
listen 80;
server_name server2.example.com;
location / {
proxy_pass http://127.0.0.1:9091;
}
}
It works well
if I delete one of them , the result does not meet my expectations.
As I delete the conf of server2.example.com
server{
listen 80;
server_name server1.example.com;
location / {
proxy_pass http://127.0.0.1:9090;
}
}
# server{
# listen 80;
# server_name server2.example.com;
# location / {
# proxy_pass http://127.0.0.1:9091;
# }
# }
When I visit server2 I think I will get 404 or 500 http code. But I get the response from server1
why?
The server_name gives way to use different virtual names and for nginx is to see which section servers.
If no section matches it then nginx will use the first declared server. Which in this case for you is server1.example.com. Or you can override that by using the default_server keyword.
server{
listen 80 default_server;
server_name server1.example.com;
location / {
proxy_pass http://127.0.0.1:9090;
}
}
You should see the below url for more details
http://nginx.org/en/docs/http/request_processing.html
1.conf
server {
listen 7070;
server_name localhost;
location / {
proxy_redirect off;
proxy_pass https://baidu;
}
}
upstream baidu {
server www.baidu.com;
}
2.conf
server {
listen 7070;
server_name localhost;
location / {
proxy_redirect off;
proxy_pass https://www.baidu.com;
}
}
why 2.conf works, but 1.conf can't proxy pass to https://baidu.com ?
it gets 502 Bad Gateway errors
If you are using SSL, you have to add port 443 to your server in your upstream directive:
https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/
server {
listen 7070;
server_name localhost;
location / {
proxy_redirect off;
proxy_pass https://baidu;
}
}
upstream baidu {
server www.baidu.com:443;
}
This is my nginx conf file.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000;
}
location /some/directory {
proxy_pass http://localhost:8998;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999;
}
}
For some reason example.com and example2.com are working, but example.com/some/directory is not.
localhost:9000 & localhost:8999: are harp.js sites, they have they own routing, and work properly, both locally and on the proxy-ed domains (example.com & example2.com).
localhost:8998: is a golang api, it works locally and also if I access example.com:8998 or example2.com:8998.
Is there something wrong with the conf?
EDIT: added more info to the question.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000/;
}
location /some/directory {
proxy_pass http://localhost:8998/;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999/;
}
}
Try this, adding the last / in the proxy_pass's should work.
I have recently started learning NGINX and encountered some problems. I am still getting and error 500, even if I have configured NGINX (1.4.6) with an error_page directive. The config works ( I get the woops.html as expected ) if I try not to send error 500 from the second upstream server. Here's my configuration file:
server {
listen 80 default_server;
root /srv/www;
index index.html index.htm;
server_name test.dev;
location / {
proxy_pass http://localhost:8080;
proxy_intercept_errors on;
error_page 500 #retry;
}
location #retry {
proxy_pass http://localhost:8081;
proxy_intercept_errors on;
error_page 500 /woops.html;
}
location = /woops.html {
root /srv/www;
}
}
server {
listen 8080;
listen localhost:8080;
server_name localhost;
root /srv/www;
location / {
return 500;
}
}
server {
listen 8081;
listen localhost:8081;
server_name localhost;
root /srv/www/app;
location / {
return 500;
}
}