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
Related
I have a rather simple question, i have an enviroment, where i have often changing subdomains, but some are also presistent, now i want to forward api.example.com to 1.2.3.4:8080 and all other incoming requests to 2.3.4.5:9090. My setup till now is that i forward all requests from the api subdomain points to 1.2.3.4:8080:
server {
listen 80;
listen [::]:80;
server_name api.example.com;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
proxy_pass http://1.2.3.4:8080;
}
}
Now i need a way to point all other subdomains to 2.3.4.5:9090.
Use default_server in listen. See docs.
server {
listen 80;
listen [::]:80;
server_name api.example.com;
location / {
proxy_pass http://1.2.3.4:8080;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location / {
proxy_pass http://2.3.4.5:9090;
}
}
All you need is to dynamically resolve your subdomains. the following config will take care of your situation.
server {
listen 80;
listen [::]:80;
server_name ~^(?<subdomain>.+)\.example\.com;
access_log /var/log/nginx/reverse-access.log;
error_log /var/log/nginx/reverse-error.log;
location / {
if ($subdomain = "api") {
proxy_pass http://1.2.3.4:8080;
}
proxy_pass http://2.3.4.5:9090;
}
}
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;
}
}
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.
There's a few similar questions on SO, but none exactly mine, and I've had no luck trying to adapt their answers so far.
I want to map the URL http://sub.example.com to https://123.12.12.12/path, such that the browser still shows the URL http://sub.example.com.
My Nginx config file looks like,
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12;
rewrite ^/$ /path last;
}
}
The routing works here, but the URL displayed is http://sub.example.com/path. How do I make it display only http://sub.example.com?
server {
listen 80;
server_name sub.example.com;
location / {
proxy_pass https://123.12.12.12/path;
}
}
Thats how it works. If proxy_pass contains locations part - current location will be replaced to specified. http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
But it's help only for http request and http redirects. If application create html with links https://123.12.12.12 - it's still unchanged. In this case you can try ngx_http_sub_module.
I did like this:
server {
listen 80;
listen [::]:80;
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name domain1;
if ($request_method ~* OPTIONS|GET|HEAD) {
return 301 https://domain2$request_uri;
}
location ~* api {
proxy_pass https://domain2$request_uri;
}
}
Because post-requests will cause a 405 error when redirecting.