i would like the site you currently have at http://ipserver:8080/project/ to be served as www.example.com. I use glassfish server. Thanks

November 7, 2016 185 views
Nginx Arch Linux

i would like the site you currently have at http://ipserver:8080/project/ to be served as www.example.com. I use glassfish server. Thanks

6 Answers

There are two ways to accomplish this, either reconfigure Glassfish to listen on port 80 or install an nginx reverse proxy in front of it.

Option 1

To have Glassfish listen on port 80:

1) Connect to the administration interface (by default on port :4848)

2) In the left menu go to Configurations

3) Then select the appropriate configuration you need to change eg server-config

4) Then go to Network Config

5) Then go to Network Listeners

6) Select the appropriate listener, probably http-listener-1

7) Change the Port value to 80

8) Save and reboot your glassfish server/instance/cluster according to your needs

Option #2

To configure an nginx reverse proxy, first install nginx. On Ubuntu this can be done with sudo apt-get install nginx. Once installed, open the file /etc/nginx/sites-enabled/default and modify it so it reads:

server {

        listen 80 default_server;
        listen [::]:80 default_server;

        server_name _;

        location / {
        proxy_pass http://localhost:8080/project/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

}

Then restart nginx for the changes to take effect:

sudo service nginx restart

You need a Reverse Proxy. As you use Nginx (assuming from tag), it's very simple.

Open your default server block (or any you want to use):

  • sudo nano /etc/nginx/sites-available/default

Find `location / block and delete any content from it. Make sure it looks like this:

...
location / {
proxy_pass http://localhost:8080/project/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
}
...

Save file, exit editor and restart nginx:

  • sudo systemctl nginx restart

You should get desired results.
To learn more about it, you can refer to this tutorial which covers both Nginx and Apache. I personally recommend Nginx as it better suit reverse proxy needs.

This tutorial will show you how to configure Nginx as both a web server and as a reverse proxy for Apache – all on one Droplet. Depending on the web application, code changes might be required to keep Apache reverse-proxy-aware, especially when SSL sites are configured. To ensure this, we will install an Apache module named **mod_rpaf** which rewrites certain environment variables so it appears that Apache is directly handling requests from web clients.

Great!! Nginx was installed already. Now, you could tell me how would the /etc/nginx/sites-enabled/default file for the case of using 2 or more projects in different urls?

Example

159.203.100 ... :8080/ Project1 to www.project1.com
159.203.100 ... :8080/ Project2 to www.project2.com

Thank you very much!!!

  • Server Blocks is need you need to setup. How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04 tutorial will explain you how to do it.

    You need to follow from Step Three: Create Server Block Files for Each Domain. Instead of serving content, you will make proxy pass locations.
    To help you, you first need to disable default server block:

    • sudo rm /etc/nginx/sites-enabled/default

    Create block for first project:

    • sudo nano /etc/nginx/sites-available/project1.com

    You need following content:

    server {
            listen 80 default_server;
            listen [::]:80 default_server;
    
            server_name project1.com www.project1.com;
    
            location / {
            proxy_pass http://ip-addr:8080/project1/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            }
    }
    

    Save it and exit editor. Repeat this for project 2:

    • sudo nano /etc/nginx/sites-available/project2.com

    And add following content:

    server {
            listen 80;
            listen [::]:80;
    
            server_name project2.com www.project2.com;
    
            location / {
            proxy_pass http://ip-addr:8080/project2/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            }
    }
    

    Note: Only one block can have default_server directive. You can go without it in first.

    Enable blocks by symlinking:

    • sudo ln -s /etc/nginx/sites-available/project1.com /etc/nginx/sites-enabled/
    • sudo ln -s /etc/nginx/sites-available/project2.com /etc/nginx/sites-enabled/

    Now restart Nginx:

    • sudo systemctl restart nginx

    It should be working as you want. :)

    When using the Nginx web server, server blocks (similar to the virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain off of a single server. In this guide, we'll discuss how to configure server blocks in Nginx on an Ubuntu...

It worked perfect. The only detail left is:

example.com works
www.example.com not work

I need some configuration to accept the www?

Thanks again!

  • You need to create CNAME record with name: www and hostname @.
    If you are using DigitalOcean to manage Domain Records do the following:
    Log In to Control Panel. Go to Networking. Select your domain. Click Add Record if Select A Record Type box is not visible. Now click on CNAME, enter www for name and @ for hostname. Click Create CNAME record.
    Maybe you'll have to wait little before DNS refreshes.
    If you don't use DigitalOcean to manage records, you need to find how to do it in your Domain DNS settings.

    If you already have www CNAME record, make sure you typed www in server_name for nginx config, e.g. server_name example.com www.example.com.

Yes, i use digital ocean. Try it. Thanks

Good morning, yesterday configure from digital ocean and nginx as they left me in the last comment, but until now I do not work the url with the www.
Am I missing something else?

I send you my settings.

In nginx in the example.com file

server {
         Listen 80;
         Listen [::]: 80;

         Server_name example.com www.example.com;

         Location / {
         Proxy_pass http: // ipServer: 8080 / Example /;
         Proxy_set_header Host $ host;
         Proxy_set_header X-Real-IP $ remote_addr;
         Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
         Proxy_set_header X-Forwarded-Proto $ scheme;
         }
}

**
In digital ocean **

*A
@ --> ipServer

CNAME
www --> @*

Have another answer? Share your knowledge.