NGINX: Proxy to blog on wordpress - wordpress

I have a wordpress based site with blog in /blog location. I wanted to re-use this blog from an other site, but I am redirected to www.example.com. My nginx config of the location is:
location ~ ^/blog {
resolver 8.8.8.8;
proxy_pass https://www.example.com$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
How should this config look like?

To make WordPress respond correctly to two different hostnames, you will need to make it hostname agnostic.
Change the WordPress configuration so that the hostname is not part of the HOME and SITEURL parameter values. For example, use a value /blog, instead of https://www.example.com/blog.
See this document for more.
Regarding the location block. It can be simplified as follow:
location ^~ /blog {
resolver 8.8.8.8;
proxy_pass https://www.example.com;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

Related

Run 3 websites on one domain using nginx

Lets say i have domain abc.com which redirects my server ip 1.2.3.4
i have 3 websites running on my server using docker container,
1st website ip 1.2.3.4:50/a
2nd website ip 1.2.3.4:60/b
3rd website ip 1.2.3.4:70/c
i can access this all websites using ip
i want to use my domain to run these three websites.
e.g.
abc.com/a should run 1st website
abc.com/b should run 2nd website
abc.com/c should run 3rd website
i am using nginx server version 1.14.1 on RHEL
Please help
i tried these configuration file
location /a {
rewrite ^/a(.*)$ http://1.2.3.4:50/a redirect;
}
location /b {
rewrite ^/b(.*)$ http://1.2.3.4:60/b redirect;
}
but it is redirecting and displaying the port in the url
Try this to solve your problem
location /a {
proxy_pass http://1.2.3.4:50;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /b {
proxy_pass http://1.2.3.4:60;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /c {
proxy_pass http://1.2.3.4:70;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
For your information, this proxy configuration will make your abc.com/a to access the root of your http://1.2.3.4:50 website. But the /a will also be passed to the http://1.2.3.4:50 so you don't have to bother add http://1.2.3.4:50/a for each proxy
You can configure it explicitly inside the nginx.conf
// Nginx.conf
http {
...
server {
server_name abc.com;
// here is location ...
}
}
Or by including configurations in sites-available, usually in the nginx.conf you can found this section:
// Nginx.conf
http {
...
include /etc/nginx/sites-available/*;
...
}
Then you can add site.conf or main.conf, you name it yourself for example in sites-available folder that has this kind of configuration:
server {
server_name abc.com;
// here is location ...
}

nginx - how to have proxy pass render a subdomain on the page?

We have a PHP server with Nginx running. On a separate server hosted by Vercel we have a Next.js app running. We've deployed the Vercel app to a subdomain of our main domain: https://vercel.employbl.com/ Our domain is hosted by Cloudflare and we linked it to Vercel by adding a CNAME record like so:
What I'm trying to do is have our main jobs page instead Render the jobs page of the Vercel app. So user enters https://www.employbl.com/jobs but the Next.js app with the matching path https://vercel.employbl.com/jobs renders instead while keeping "employbl.com/jobs" as the URL.
This is the Nginx config we have currently using proxy_pass. Unfortunately I'm getting a 404 error from Vercel when I navigate to "employbl.com/jobs" using this Nginx config:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /jobs {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com/jobs;
}
This renders the page from Vercel: 404 - Code: DEPLOYMENT_NOT_FOUND but the site is working at the URL provided in the nginx config.
How can I configure Nginx so that when the user navigates to "employbl.com/jobs" the https://vercel.employbl.com/jobs page renders?
Try it like this, with trailing slash after the location directive (/jobs/). Then take /jobs off the proxy URL. I think your location path /jobs is being handed to the proxy_pass and ending up doubling the /jobs/ part somehow. Because with no trailing slash it passes that forward. So try:
location /jobs/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com; #removed jobs
}
Edit: On my server I can only get it to work this way. Both location directive and proxy URL end in a slash. From what I've read, this should make it drop the location directive path, instead of passing it on.
location /jobs/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass https://vercel.employbl.com/jobs/;
}
We ended up getting it working like this. Needed a separate entry to point to files for the _next directory that holds our assets. I think the primary fix was having resolver 8.8.8.8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /jobs {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://vercel.employbl.com/jobs;
}
location ~ ^/_next/(.*)$ {
resolver 8.8.8.8;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass https://vercel.employbl.com/_next/$1;
}

How do I rewrite request_uri variable in a location match

Java backend give me several interfaces like:
/admin/login
/client/query
/agent/update
I am a frontend engineer and I have to use these interfaces with ajax. Because we will deploy our projects on the same server, so I need to use a nginx proxy to direct those ajax request to the java service. But as you can see the interfaces start with different paths, so I prefixed them with "/api" in my code, and also configure nginx as below
axios.post('/api/admin/login'),
axios.post('/api/client/query'),
axios.post('/api/agent/update')
location /api {
proxy_pass http://127.0.0.1:8080$request_uri;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
here my problem come:
Actually the java service get the request path: /api/admin/login, not /admin/login, so they can not handle that request. Can I rewrite my request_uri in nginx so that java get requests without /api prefix?
You don't need any rewrites for this. Use following location block:
location /api/ {
proxy_pass http://127.0.0.1:8080/;
proxy_set_header Host 127.0.0.1;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
See more info about slashes in proxy_pass directive here.
Copy pasted and adapted from a different question but should do the trick.
location /api {
rewrite ^/api(/.*)$ $1 last;
}

Use Wordpress Inside Laravel Nginx

I'm trying to have Wordpress installed in other folder than "/public" on Laravel project.
The laravel path:
/home/forge/domain.com/
Wordpress path:
/home/forge/blog.domain.com/
But I want to use this url and not as subdomain:
domain.com/blog
You can use a location section in your nginx configuration file:
location ^blog/ {
root /home/forge/blog.example.com;
//your regular nginx configuration
}
You may also use a reverse proxy. This means you have to put the blog on a different URL (like blog.domain.com) and then point blog/ to that URL. For example:
location ^blog/ {
sub_filter_types *;
sub_filter_once off;
sub_filter 'blog.example.com' 'www.example.com/blog';
proxy_pass http://blog.example.com/;
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 Accept-Encoding "";
proxy_redirect http://blog.example.com http://www.example.com;
}
The subfilter rewrites all URLs in your html output. The proxy_redirect makes sure cookies and headers are corrected as well.

Avoid duplication in password protect URL with proxy in nginx

I have a Flask application served using gunicorn, and with NGINX on top of it. I want to use Basic Authentication (user/password) to protect all URL's starting with /admin, which is the back office, but still continue serving all other URLs with gunicorn without password.
Here is my current NGINX config:
server {
listen 80;
server_name example.com;
charset utf-8;
location / {
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /admin {
auth_basic "Administrator Login";
auth_basic_user_file /home/app/.htpasswd;
# the following four directives are duplicated :(
proxy_pass http://localhost:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I don't duplicate the proxy_* directives in the second location block, then the URLs starting with /admin doesn't get forwarded to gunicorn and I get a 404.
Is there any way to avoid the configuration duplication? I tried location nesting but apparently in the end NGINX only "executes" a single location block.
The proxy_pass must be within the location block. However, there's no need to duplicate the proxy_set_header directives, they can be moved into the server block. So your mistake was simply the assumption that proxy_pass could live in the server block :-)

Resources