Nginx vhosts per path - nginx

How do I setup nginx conf so that "blah.com/website1" and "blah.com/website2" etc can point to separate websites that are running on the machine (vhost?)?
E.g. localhost:3000 is website1 and localhost:3001 is website2.
So when a user points their browser at blah.com/website1 they access the website localhost:3000 and localhost:3001 for website2?
I've tried a reverse proxy but that can't map paths like "blah.com/website1" and only seems to handle the server name of "blah.com".
Update:
So I've tried a config like this to use reverse proxy, but all assests on the website are not loading as they are trying to load from the domain's root. Probably because it's not a vhost? The page loads but assets are trying to load and 404ing, e.g: http://blah.com/static/css/styles.css
server {
listen 80;
server_name blah.com;
location /website1/ {
proxy_pass http://localhost:3001/;
}
}

Related

One Next.js instance for two domains - how to redirect?

Let's say I have a business offering two different types of services. I would like to have two domains, eg. example-shop.com and example-rental.com and have one Next.js server instance for both of them (so that they can use common components, styles etc.). How should I configure Next.js or the server (eg. with Nginx) to make it work?
What I've tried:
In my Next.js project, I created the subfolders /pages/shop/ and /pages/rental/. I built the project and run it on the server on port 3000. Then, I set up an Nginx reverse proxy on my server with the following redirects:
server {
listen 80;
server_name example-shop.com;
location / {
proxy_pass http://localhost:3000/shop/;
}
}
server {
listen 80;
server_name example-rental.com;
location / {
proxy_pass http://localhost:3000/rental/;
}
}
The HTML file loads correctly, but all the other files (js, css, images) are also redirected to /shop or /rental and end up with 404. How can I fix this?
My ideas:
only redirect HTML files to localhost:3000/shop or localhost:3000/rental and redirect all the other requests to localhost:3000/ (but how?)
set up Next.js so that all the resource files are stored under a separate directory - then I could do
location /resources {
proxy_pass http://localhost:3000/;
}
... but how?

nginx reverse to all pages

I have the following configuration that works for only 1 page:
location /mypage.html/ {
proxy_pass http://${remote_server}/;
}
When I am trying to navigate to other pages on the remote server, I get page not found.
Is there any way to keep the reverse proxy open for all pages on the remote server?
This will match everything, technically everything that starts /, and route the same request to your remote server:
location / {
proxy_pass http://${remote_server}/;
}

Wordpress & Nginx with Docker: Static files not loaded

I'm using Docker to serve my simple WordPress website. A nginx container and a wordpress container. Simple setup:
upstream wordpress_english {
server wordpress_en:80;
}
server {
listen 80;
server_name my_domain.com www.my_domain.com;
location / {
proxy_pass http://wordpress_english;
}
}
Problem: Static files (css, js and images) are not loaded.
The output from the browser console shows a 404:
http://wordpress_english/wp-content/themes/twentyfifteen/genericons/genericons.css?ver=3.2
Its easy to spot the problem: The browser looks for the static files at wordpress_english (the nginx upstream name), instead of my_domain.com
How can I fix this problem?
This is not a nginx problem, but a WordPress problem.
Solution:
In wp-config.php, add the following two lines:
define('WP_HOME','http://my_domain.com');
define('WP_SITEURL','http://my_domain.com');
During the WordPress installation, WordPress automatically sets WP_HOME to nginx upstream name. The above solution overwrites the default setting.
Seems to be an issue in your nginx config file.
When declaring your server my_domain you provide location / with proxy_pass wordpress_english. I don't know a lot on nginx, but I don't see any declaration of path in your server my_domain and is root is linked to wordpress_english. Seems normal that he is looking for files in wordpress_english and not in you server. (In fact, I guess he is looking in your server but your server tells to look in wordpress).
Not sure about it cause I don't know well nginx and proxy_pass functions.

Nginx does not serve the flask pages and shows the default static page

I have a flask app running with gunicorn and nginx on Ubuntu 14.04 using AWS EC2. I have deleted default site from /etc/nginx/sites-available and /etc/nginx/sites-enabled. In those two folders, there is only one file: flasky - my nginx file as below:
server {
listen 80;
location / {
include proxy_params;
proxy_pass http://unix:/tmp/flasky.sock;
}
When I enter the IP of the server in the browser, the default Nginx static page shows up. If I go to /auth/login, the correct page served by Flask shows up properly.
If the change the port from 80 to 8080, restart Nginx, enter http://ip-address:8080 then all Flask pages work well. I don't know how to fix this for port 80. Please help! Thanks!
UPDATE: I just found out that if I use the AWS Public DNS: http://ec2-50-112-125-180.us-west-2.compute.amazonaws.com, it works. But if I use the corresponding Elastic IP: 50.112.125.180 it shows the nginx default page. Anyone knows why?
Search your nginx.conf for the word include
maybe it's including other directories for config files.
Also, there may be other config blocks with
location /
that is superseding this section that also have listen 80.

Nginx Proxy for PlayFramework from port to path prefix

I have a Play application listening on a local port :9000. There are other applications running.
I would like to server this application at a path like:
http://myhost/this-play-app -> localhost:9000
So that other apps could be nested at other paths.
I've tried the basic proxy_pass but it doesn't seem to work.
server {
listen 80;
server_name myhost;
# MMC Tool
# ----------------------------------------------------
location /this-play-app {
proxy_pass http://localhost:9000;
}
}
The play app seems to forward to the root. Is there a way to trick the play app to work within the /this-play-app path ?
Like /this-play-app/some-controller instead of /some-controller ?
Thanks
Using apps in folders isn't comfortable idea - you would need to at least prepare some dedicated config and change it each time when changing the location.
Instead as other suggested you should use subdomains, in this case each app behaves exactly the same as in the root domain and even if you will need/want to change that domain all you'll need will be change in the nginx's config.
Typical nginx's config looks like
upstream your_app {
server 127.0.0.1:9000;
}
server {
listen 80;
server_name your-app.domain.com;
location / {
proxy_pass http://your_app;
}
}
Most probably on some VPS or shared hosts you'll need to add the subdomain by some kind of admin's panel - on localhost just need add the subdomain to the hosts file.
Edit if using subdomain is not possible anyway (pity) you can workaround it anyway by config, in nginx use (as you did in question:
...
location /this-play-app {
proxy_pass http://your_app;
}
...
and then add this line into your application.conf (Play 2.1+)
application.context = "/this-play-app"
Or this in case of Play 2.4+ (info)
play.http.context = "/this-play-app"

Resources