Rewrite location to subdomain in nginx
-
12-04-2021 - |
Question
How can I rewrite a subdomain to an application in NGINX?
My configuration is
server {
listen 80;
server_name domain.com www.domain.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9001;
uwsgi_param UWSGI_PYHOME PATH;
uwsgi_param UWSGI_SCRIPT wsgi;
uwsgi_param UWSGI_CHDIR PATH;
}
location /app1 {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9001;
uwsgi_param UWSGI_PYHOME PATH2;
uwsgi_param UWSGI_SCRIPT wsgi2;
uwsgi_param UWSGI_CHDIR PATH2;
uwsgi_modifier1 30;
}
}
I want to when access app1.domain.com I got rewrited to domain.com/app1. How can I do this?
Thanks in advance.
Solution
I don't think you need to - you can add another server section like:
server {
server_name app1.domain.com;
location / {
<uwsgi as before>
}
}
rather than a location /app1
followed by a redirect. Hope that helps.
OTHER TIPS
You can either just add the sub-domain to the server name in which case it will just work as the domain but the user will not be redirected and the address bar will show the sub-domain:
server {
listen 80;
server_name domain.com www.domain.com app1.domain.com;
location / {
include uwsgi_params;
...
}
}
Alternatively, you can create a specific server for the sub-domain which redirects to the domain:
server {
listen 80;
server_name app1.domain.com;
rewrite ^ http://domain.com$request_uri? permanent;
}
server {
listen 80;
server_name domain.com www.domain.com;
location / {
include uwsgi_params;
...
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow