I would like to use NGIX as a reverse proxy to display different URLs, pointing on the same server, but on different subfolders... The web server is different than the one on which NGIX is running.
here is my web tree, server on the same LAN (192.168.1.10 let's say, port 80...)
/www
|- site1
|- css
|- js
|- site2
|- site3
I want to setup NGIX so when I type www.site1.com on my browser, it shows 192.168.1.10/www/site1
Is that possible ?
here is what I've done, but it didn't work :
server {
listen 80;
server_name www.site1.com;
root /www/site1;
location / {
proxy_pass http://192.168.1.10;
}
}
on the nginx server (A) you can add below configuration on same configuration(V-host) file or you can create different configuration files for each server tags under /etc/nginx/conf.d/
I have consider webserver ip is 192.168.1.10 and running port is 80
server {
listen 80;
server_name www.site1.com;
location / {
proxy_pass http://192.168.1.10;
root /www/site1;
}
}
server {
listen 80;
server_name www.site2.com;
location / {
proxy_pass http://192.168.1.10;
root /www/site2;
}
}
server {
listen 80;
server_name www.site3.com;
location / {
proxy_pass http://192.168.1.10;
root /www/site3;
}
}
If I understood correctly, you need to make the change in the web server (let's say server A). Not in the server where Nginx is running (server B).
'root' directive you have mentioned refers to server B. Nginx will simply pass all requests coming to www.site1.com to server A. There you have to configure the server to deliver content based on host name.
If server A is also running Nginx, you can have a configuration like below in server A.
server {
listen 80;
server_name www.site1.com;
root /www/site1;
}
server {
listen 80;
server_name www.site2.com;
root /www/site2;
}
server {
listen 80;
server_name www.site3.com;
root /www/site3;
}
Related
For example, I have a server with 2 network interface, one for public ip and one for private ip. And write 2 nginx configuration file:
cat /etc/nginx/sites-enabled/siteA.sample.edu.cn
server {
listen 80;
server_name siteA.sample.edu.cn;
...
location / {
root /var/lib/www/siteA.sample.edu.cn;
index index.html index.htm index.php;
}
}
cat /etc/nginx/sites-enabled/siteB.sample.edu.cn
server {
listen 80;
server_name siteB.sample.edu.cn;
...
location / {
root /var/lib/www/siteB.sample.edu.cn;
index index.html index.htm index.php;
}
}
As long as they both listen on 80 without ip restriction, they can work together well. Setting local dns for siteA and siteB with the same ip 172.16.0.1, I can visit different site with those url.
But when setting explict listen ip to one site:
cat /etc/nginx/sites-enabled/siteA.sample.edu.cn
server {
listen 172.16.0.1:80;
server_name siteA.sample.edu.cn;
...
}
}
cat /etc/nginx/sites-enabled/siteB.sample.edu.cn
server {
listen 80;
server_name siteB.sample.edu.cn;
...
}
}
Then I cannot visit siteB.sample.edu.cn anymore. Using url http://siteB.sample.edu.cn will finally reach the siteA.sample.edu.cn.
So how to stop such strange redirection? It seems that server with explicit listen ip has higher priority?
This behaviour is documented here.
You could try using two listen directives in site B's server block.
For example:
server {
listen 172.16.0.1:80;
listen 80;
...
}
Or:
server {
listen 172.16.0.1:80;
listen <otherIP>:80;
...
}
I have my flask application deployed on Nginx over my VM.
Everything is deployed Ok and I can request my apis on http://my.ip.number (I have a public IP)
But when I run Ngrok (I need https and I don't have a domain name to generate a SSL certificate), the URL https//number.ngrok.io shows me the Nginx home page (Welcome to Nginx) instead my webapp.
Why is this happening?
P.D: When I run "curl localhost" I get the Nginx Welcome Page but when I exec "curl -4 localhost" I get my webapp home page
etc/nginx/site-available/myproject
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name public.ip;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Any request coming in from ngrok, has the Host header set to the ngrok URL. The behaviour of nginx would be to try and match one of the server blocks in your configuration above, and default to the first one if no server_name matches the Host header.
However, I'm guessing there's another configuration file at /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/0-default which has a listen directive with default_server set. That will be catching these requests and serving the "Welcome to nginx!" page.
I suggest you look for that file, and remove it which should solve the issue.
However you could also simplify the above configuration and simply have:
server {
listen 80;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Provided there's not another server block hiding somewhere else in the configuration with a directive like listen 80 default_server; then this should catch all requests.
For more info see: How nginx processes a request
I have a 2 servers :-
Server 1 : NGINX Reverse Proxy.
Server 2 : NGINX with 5-6 websites ( different domains )
So basically, all users will come to Server 1 which will proxy_pass the traffic to Server 2 and get the response back. Server 1 will also do Caching, WAF etc.
Here is my configuration for Server 1 :-
server {
listen 80;
server_name example.com www.example.com;
location ~* {
proxy_pass http://mysite:80;
}
}
server {
listen 80;
server_name server.com www.server.com;
location ~* {
proxy_pass http://mysite:80;
}
}
In my Server 2, in virtual.conf of NGINX, i have the following config :
index index.php index.html;
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/websites/example/;
include location-php;
}
}
server {
listen 80;
server_name server.com www.server.com;
location / {
root /var/www/websites/server/;
include location-php;
}
}
However whenever i go to http://example.com or http://server.com ( directed via Sever 1 acting as Reverse Proxy ), it shows the Server 2's Default NGINX Page. I am not sure what am I doing wrong. Also is this type of setup a proper way of doing things ?
This is your host problem.
Due to your upstream name is mysite, so the host name in upstream request is mysqsite too.
So the host isn't matched by the backend servers.
You can solve the problem like this by adding the directive before proxy_pass:
proxy_set_header Host server.com
I have a Rails app and a Ghost blog installed on my server with Nginx as the web server.
I want blog.example.com to point to my blog and everything else to point to main App.
My nginx.conf looks like this:
server {
listen 80;
server_name blog.example.com;
root /root/ghost;
location / {
...blah
}
}
server {
listen 80;
server_name example.com, *.example.com;
root /home/example/current/public/;
gzip_static on;
location / {
...blah
}
}
However if I omit the www in the domain, it goes to the blog instead of app.
What am I doing wrong?
server_name is space-separated, not comma-separated. It should be server_name example.com *.example.com;.
I'm using nginx as a load balancer to 4 internal server instances. The below nginx.conf will work correctly only for www.mydomain.com . But not for mydomain.com or http://mydomain.com.
upstream mydomain{
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
}
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://mydomain;
}
}
It's normal that it doesn't work for mydomain.com because of server_name www.mydomain.com.
I'm not an nginx expert, but try omitting server_name.
I think you can try this:
server {
listen 80;
server_name www.mydomain.com mydoamin.com;
location / {
proxy_pass http://mydomain;
}
}
At least I've found this solution in docs
all webservers will only work for the domain you configure. In this case, the only domain you added is www.mydomain.com, so it is only going to "work" for the address www.mydomain.com.
If you want all subdomain to work, you need a wildcard character in front of mydomain.com as the following:
server {
listen 80;
server_name .mydomain.com;
location / {
proxy_pass http://mydomain;
}
}
Notice the . before mydomain.com.