rewrite is not working as desired for redirect URL in NGINX

Viewed 576

I have an Nginx problem and after reading a lot and trying multiple combinations, I do not find the solutions to my problem.

I have a Jira server behind a reverse proxy using Nginx. My problem is that I want to have jira.mydomain.com as the default url but when using support.mydomain.com I want it to redirect to https://jira.mydomain.com/servicedesk/customer/portal/1 because is where the Service Desk. This is what I do not make.

The result that I have now is that both jira.mydomain.com and support.mydomain.com go to the same place which probably is the normal behavior and I do not know how to configure it but if anyone can help would be very appreciated.

Thanks to all of you and the hivemind.


This is what I have right now

/etc/nginx/conf.d/default.conf

upstream jira {
       server jira_ip_adress:8081;
}

server {
    server_name jira.mydomain.com;
    server_name support.mydomain.com;
    rewrite ^/support.mydomain.com https://jira.mydomain.com/servicedesk/customer/portal/1;
    [...]
    location / {
        proxy_pass http://jira;
        [...]
        }
2 Answers

Nginx uses the server_name directive to match the domain name part of a URL. You need to use two server blocks, one for each of the domain names.

For example:

server {
    server_name support.mydomain.com;
    return 301 https://jira.mydomain.com/servicedesk/customer/portal/1;
}
server {
    server_name jira.mydomain.com;
    ...
}

Obviously, if these are https services, you will need to add the appropriate listen statements to both server blocks.

Use nginx -T (uppercase T) to test the configuration file and view the entire configuration across all included files.

Many many thanks. I tried this yesterday but I had the next error when using nginx -T

nginx: [emerg] unexpected "E" in /etc/nginx/conf.d/default.conf:183
nginx: configuration file /etc/nginx/nginx.conf test failed

I have tried again after your help, and you spotted me in the right direction because today have been able to know what the error was telling to me and fixed.

So know, with your suggestion, is working like a charm.

Thanks a lot!

Related