nginx config to create subdomain from domain proxy_pass - nginx

I am trying to setup a domain and subdomain for my domain name in nginx server for www.example.com/users to www.people.example.com
i am able to configure subdomain but when i typed url in browser of subdomain url it is redirecting to www.example.com/users
But i need it to be in subdomain
is there anything that i missed in nginx config? please provide me any suggestion nginx config for subdomain
server
{
server_name people.example.com www.people.example.com;
error_log /var/log/nginx/error.log;
location / {
proxy_pass http://example.com/users$request_uri;
proxy_set_header "Host" $host;
proxy_redirect http://$host/users http://$host;
}
And also suggest me config for www.example.com to block access via www.example.com/users but can be accessed via www.people.example.com

Try redirecting from a separate server stanza. Then remove the proxy_* commands from the people.example.com server.
server {
server_name example.com www.example.com;
location = /users {
return 301 http://people.exxmple.com;
}
}

Related

How can a request resolve to a different domain while keeping URI information?

I have 2 domains domain.xyz and domainxyz.com
I have the boilerplate for a dynamic routing service set up on domain.xyz which serves domain.xyz/example-uri.
I would like to make domainxyz.com/example-uri resolve to domain.xyz/example-uri
On the server I am using Nginx to direct requests on port 80 to the React app
instead of using a dns redirect to resolve the .com to the .xyz (which is what I was initially doing), direct both domains to the nginx server. Then configure the nginx server like this:
source/explanation: https://www.nginx.com/blog/creating-nginx-rewrite-rules/
server {
listen 80;
server_name www.domainxyz.com domainxyz.com;
location / {
return 301 $scheme://domain.xyz$request_uri;
proxy_pass http://localhost:3000;
}
}
server {
listen 80;
server_name www.domain.xyz domain.xyz;
location / {
proxy_pass http://localhost:3000;
}
}

Hide Domain Folder via Nginx reverse proxy

I am trying to remove a part of my URL and make an app available on sub-domain level.
Users shall access mail.example.com. The mailing app is currently available at mail.example.com/mail. How can I proxy user, opening mail.example.com, while accessing mail.example.com/mail via NGINX?
Following config fails for CCS and other files:
server {
listen 443 ssl http2;
server_name mail.example.com;
location / {
proxy_pass https://1.2.3.4/mail$request_uri;
proxy_http_version 1.1;
proxy_redirect off;
}
}

Stop nginx redirect to proxy_pass location

I have two domains: abc.com and def.com. I am running a Django application behind nginx. Single django application has two apps running under different URL, app1 is running under url: localhost/corp and app2 is running under url: localhost:/service.
Now I want to server /corp on abc.com and /service on def.com domain.
For this I have done this NGINX configuration:
server {
listen 80;
server_name abc.com;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/site/site.sock:/corp;
}
}
server {
listen 80;
server_name def.com;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/site/site.sock:/service;
}
}
But the configuration is not working this way, when I visit abc.com, I get redirected to abc.com/corp and when visiting def.com, its redirected to def.com/service. Instead, I want /corp content to be served on abc.com root and /service content on def.com root domain itself. How can I modify this configuration to achieve this?

change the host header after the backend server responds

I have a proxy setup to receive a client request for www.example.com from external.example.com (www.example.com is a tab on external.example.com) . the proxy recieves the request and sends to www.example2.com to the backend server with multiple websites with host headers. now when www.example2.com responds with path and query strings ex : https://www.example2.com/results . I want this intercepted by the proxy and have the proxy change just the header to http://www.example.com without going into a loop and display results . I have tried proxy_set-header host and proxy_redirect and both fail. below is my config :
server {
listen 80;
listen [::]:80;
server_name www.example.com;
#root /var/www/www.example.com;
#index index.html;
location / {
proxy_pass http://www.example2.com;
proxy_set_header Host http://www.example.com;
OR
Proxy_redirect https://www.example2.com$1 http://www.example.com
}
}
I was doing a proxy_pass to a URL which was behind an aws elb . The elb was stripping out the set host header commands and proxy redirects . IO changed it to a prticular instance and it started working as expected. thanks for your help #nbari

Setting up nginx to support custom domain name

I have a Django web application hosted on a VM with the Debian-based Ubuntu as the OS, and nginx reverse proxy + gunicorn as the webserver.
The DNS of this web application is myapp.cloudapp.net. I also have a ccTLD mydomain.pk I need to be configured as a custom domain name for this web application.
My original registrar only supported nameservers. Thus I made an account on dns.he.net (a free DNS hosting provider) to host my nameservers, and set up the CName for my machine.
My problem is that once I set up the CName to point to my web app's DNS, entering mydomain.pk in the browser merely shows me a generic Welcome to ngnix! page. Whereas, entering myapp.cloudapp.net (or myapp.cloudapp.net:80) in the browser correctly opens up the web application. Why isn't setting up the CName working?
I've talked to the support staff at dns.he.net - I've been told my CName is set up correctly, and that there might be some problem with my nginx configuration.
For instance, here's my etc/nginx/sites-available/myproject file:
server {
listen 80;
server_name myapp.cloudapp.net;
charset utf-8;
underscores_in_headers on;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/myuser/projectpk/project;
}
location /static/admin {
root /home/myuser/.virtualenvs/projectpk/local/lib/python2.7/site-packages/django/contrib/admin/static/;
}
location / {
proxy_pass_request_headers on;
proxy_buffering on;
include proxy_params;
proxy_pass http://unix:/home/myuser/projectpk/project/project.sock;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/myuser/projectpk/project/templates/;
}
}
Remove the server_name line, it's not needed in nginx unless you want to serve different content depending on the host name you receive.
If you remove that line, nginx will answer any request that arrives to your server at the proper port (80 in this case), coming with myapp.cloudapp.net or mydomain.pk in the Host header.
This assumes that there is no other configuration in /etc/nginx/sites-enabled that would catch the requests.

Resources