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.
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.
In my nginx server , I want to do this:
mydomain.com/api/xxxx
redirect to mynewdomain.com/api/testing/xxxx
I try to do by different ways but no ones works for me, always return a 502 error;
First way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
return 301 https://mynewdomain.com/api/testing/$uri;
}
}
Second way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/(.*) {
proxy_pass https://mynewdomain.com/api/testing/$args_v;
}
}
And I tryed to only redirect to specific direction and this works, but i don't want to hardcode all petitions to redirect,
Static redirect
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
proxy_pass https://mynewdomain.com/api/testing/redirect1;
}
Any help for make this dynamic?
location ~ ^/api/(.*) {
return 301 https://mynewdomain.com/api/testing/$1;
}
Try this, because $uri also includes /api.
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;
}
}
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
like these:
test1.sec.com 192.168.1.8:8001<br>
test2.sec.com 192.168.1.8:8002<br>
test3.sec.com 192.168.1.8:8003<br>
http://www.sec.com/test1/ 192.168.1.8:8001<br>
http://www.sec.com/test2/ 192.168.1.8:8002<br>
http://www.sec.com/test3/ 192.168.1.8:8003<br>
how to config the nginx.conf ?
You haven't provided much information, but based on what you gave, this should work:
server {
listen 80;
server_name test1.sec.com;
location / {
proxy_pass http://192.168.1.8:8001;
}
}
server {
listen 80;
server_name test2.sec.com;
location / {
proxy_pass http://192.168.1.8:8002;
}
}
server {
listen 80;
server_name test3.sec.com;
location / {
proxy_pass http://192.168.1.8:8003;
}
}
Then, for your www.sec.com, you'll need to add separate location blocks to catch the /test/ URIs.
server {
listen 80;
server_name www.sec.com;
location /test1/ {
redirect 301 $scheme://test1.sec.com;
}
location /test2/ {
redirect 301 $scheme://test2.sec.com;
}
location /test3/ {
redirect 301 $scheme://test3.sec.com;
}
#Rest of your config here...
}
Note: You have to specify your test location blocks before your root (/) unless you use a modifier to give them precedence.