Skip to content

Instantly share code, notes, and snippets.

@jspw
Created June 22, 2023 19:22
Show Gist options
  • Save jspw/0e381dce4e773e340596803233dcb365 to your computer and use it in GitHub Desktop.
Save jspw/0e381dce4e773e340596803233dcb365 to your computer and use it in GitHub Desktop.
Config nginx with symlink

Yes, you can create a separate configuration file for your React app in the /etc/nginx/sites-available directory. Here's how you can do it:

Step 1: Create a new NGINX configuration file Run the following command to create a new configuration file for your React app:

sudo nano /etc/nginx/sites-available/my-react-app

This will open the Nano text editor with a new file.

Step 2: Configure the NGINX server block Inside the file, add the following configuration for your React app:

server {
    listen 80;
    server_name example.com;  # Replace with your domain or IP address

    location / {
        proxy_pass http://localhost:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Make sure to replace example.com with your domain or IP address.

Step 3: Save and close the file Press Ctrl + X to exit Nano, then press Y to save the changes, and finally press Enter to confirm the filename.

Step 4: Create a symbolic link Create a symbolic link to enable the configuration by running the following command:

sudo ln -s /etc/nginx/sites-available/my-react-app /etc/nginx/sites-enabled/

Step 5: Test the NGINX configuration Before restarting NGINX, it's a good idea to test the configuration for any syntax errors. Run the command:

sudo nginx -t

If the test is successful, you should see a message indicating that the configuration is valid.

Step 6: Restart NGINX Finally, restart NGINX to apply the new configuration:

sudo service nginx restart

Now NGINX should be configured to serve your React app. Accessing your domain or IP address should route the requests to the React app running on port 3000.

You can repeat the same process to create another configuration file for your Express app in a separate file within the sites-available directory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment