Redirect from apache2 to nginx on different port without changing url - nginx

I'm trying to redirect a user to an Nginx Server using my apache2 server when using a specific subdomain.
The apache2 server runs on port 80 and the nginx on port 81
So far it works with one exception, the apache2 redirects to game.domain.com:81 instead of just game.domain.com
How can I fix this? Those are my configurations
<VirtualHost *:80>
ServerName game.domain.com
ProxyPass "/" "http://localhost:81/"
ProxyPassReverse "/" "http://localhost:81/"
</VirtualHost>
<VirtualHost *:80>
ServerName catchall
<Location />
Order allow,deny
Deny from all
</Location>
</VirtualHost>
<VirtualHost *:80>
ServerName admin.domain.com
DocumentRoot /var/www/html/dir
<Directory /var/www/html/dir>
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
server {
listen 81 default_server;
listen [::]:81 default_server;
server_name _;
return 412;
}
server {
listen 81;
server_name game.domain.com;
root /var/www/node/dir;
location / {
try_files $uri $uri/ =404;
}

Related

Migrate existing worpress sites behind a reverse proxy (lxd)

I want to put a proxy container with Nginx to handle traffic to my other containers with WordPress sites.
I am currently using the following setup:
Internet <--> webservices.lxd (added port 80 && 443 to the container) <--> example.com (apache2)
The new setup should look like this:
Internet <--> proxy.lxd (added port 80 && 443 to the container)(nginx) <--> webservices.lxd <--> example.com (apache2)
So in short: I would like to put a reverse proxy container handling all the traffic between lxd containers.
Here is the Nginx reverse proxy config file:
server {
server_name example.com;
location / {
include /etc/nginx/proxy_params;
proxy_pass http://webservices.lxd;
}
real_ip_header proxy_protocol;
set_real_ip_from 127.0.0.1;
listen [::]:443 ssl proxy_protocol; # managed by Certbot
listen 443 ssl proxy_protocol; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 proxy_protocol;
listen [::]:80 proxy_protocol;
server_name example.com;
return 404; # managed by Certbot
}
Here is the apache2 site config from within the (current with no proxy) web services container:
root#webservices:/etc/apache2/sites-available# cat example.com.at-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot "/var/www/example.com/htdocs"
ServerName example.com
UseCanonicalName On
# Security Section
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains"
# Security Section Ende
<Directory "/var/www/example.com/htdocs/">
Options MultiViews FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
TransferLog /var/log/apache2/example.com_access.log
ErrorLog /var/log/apache2/example.com_error.log
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias www.example.com
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
ServerAlias *example.com
<Location "/">
Redirect permanent "https://%{HTTP_HOST}%{REQUEST_URI}"
</Location>
</VirtualHost>
</IfModule>
My questions are:
A. What do I need to change to run the new proxy setup?
B. Do I need to change something else (WordPress wp-config.php or else)?
C. Besides: the database server is also running within the web service container. Should I also make a new container for the databases?
Here are the two tutorials I already tried but did not work for my "migration" case:
https://www.linode.com/docs/guides/how-to-set-up-multiple-wordpress-sites-with-lxd-containers/
https://www.linode.com/docs/guides/beginners-guide-to-lxd-reverse-proxy/

Infinite 301 redirects on 2 sites

I have two websites:
http://forkandspoon.pl/ - Worpress
http://lunch.forkandspoon.pl/ - custom code (old Kohana framework)
Not sure what happened but everything was working fine until a few days ago. The sites are no longer accessible. There is an infinite number of 301 redirects.
Site 1 - Weird part is, admin panel is accessible and is working but the main website isn't. Wordpress is updated to the latest version, I did update plugins and themes but I have not done any development.
I disabled all plugins, switched to a different theme, deleted .htaccess file (which is standard, no weird code in there). Nothing helps.
Site 2 - I have not touched this site in months.
I went through the code for both sites, I compared the code from server with local copies. Everything looks to be in order.
I have Apache and Nginx on that server and the configuration has not changed at all. I have other websites hosted on the same server and they're not affected.
Cloudflare handles DNS, which I also checked and everything looks fine there.
What can be happening here? I'm out of ideas. Any clues?
As requested, here's Nginx conf:
server {
listen 80;
root /home/forkandspoon/forkandspoon.pl;
index index.php index.html index.htm;
server_name forkandspoon.pl www.forkandspoon.pl;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
root /home/forkandspoon/lunch.forkandspoon.pl;
index index.php index.html index.htm;
server_name lunch.forkandspoon.pl www.lunch.forkandspoon.pl;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~ /\.ht {
deny all;
}
}
and Apache
<VirtualHost *:8080>
ServerAdmin myemail#domain.com
DocumentRoot /home/forkandspoon/forkandspoon.pl/
ServerName forkandspoon.pl
ServerAlias www.forkandspoon.pl
ErrorLog logs/forkandspoon.pl-error_log
CustomLog logs/forkandspoon.pl-access_log common
<Directory /home/forkandspoon/forkandspoon.pl>
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerAdmin myemail#domain.com
DocumentRoot /home/forkandspoon/lunch.forkandspoon.pl/
ServerName lunch.forkandspoon.pl
ServerAlias www.lunch.forkandspoon.pl
ErrorLog logs/lunch.forkandspoon.pl-error_log
CustomLog logs/lunch.forkandspoon.pl-access_log common
<Directory /home/forkandspoon/lunch.forkandspoon.pl>
Options +Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
Note,
I also disabled Cloudflare proxy as per one of the comments. It may take a few minutes to take effect.
As it turns out this is a know issue https://wordpress.org/support/topic/45-causes-infinite-redirect-on-static-front-page/ apparently introduced in Wordpress 4.5 where if your homepage is a static page you'll be infinitely redirected.
The fix for that can be found in the above link.
Thank you to #DusanBajic for pointing me in the right direction :)

Local nginx custom url

Can someone help me convert this apache file to nginx. The bit I am after the most is 'neptix' as the sitename.. So in a browser I could go to something like: neptix/about-us, neptix/contact-us. NOTE: No .com
<VirtualHost 127.0.0.1>
ServerName neptix
ServerAlias *.neptix
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass /api http://52.35.118.165/api
ProxyPassReverse /api http://52.35.118.165/api
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
<Location /api>
       Order allow,deny
Allow from all
</Location>
DirectoryIndex index.html index.php
</VirtualHost>
May be this will help (it probably needs some small tuning):
server {
listen 80;
server_name .neptix;
index index.php index.html index.htm;
location /api {
proxy_pass http://52.35.118.165/api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}

Create Virtual Host on Nginx Server

I have been working a lot with LAMP lately, But now, I've started to work with nginx. So, I installed nginx and wanted to create virtual host, Since the folder structure of LAMP is different to that of stand alone nginx folder structure, and unable to understand how to create virtual host.
I have visited few links like:
Tutorial 1, Tutorial 2 - Not Useful coz it is for standalone nginx
There's 1 Question on SO, it is also a kind of similar to my situation, but is unanswered.
Default nginx config contains these lines to check sites-enabled directory:
http {
# ...
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
I think you can edit config supplied by bitnami at /opt/bitnami/nginx/conf/nginx.conf to add these paths (or any other) and follow usual tutorials.
Define a file in your project directory for e.g. vhost.conf
write following code:
server {
listen 8080;
root "your_project_directory";
server_name your_host_name;
index index.php;
allow 127.0.0.1;
deny all;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/**your_bitnami_install_directory**/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
include this file in: your_bitnami_install_directory/nginx/conf/bitnami/bitnami-apps-vhosts.conf
include "your_project_directory/vhost.conf";
restart the nginx
Apache Virtual Host [/etc/apache2/sites-available/000-default.conf]
<VirtualHost *:8080>
ServerName abc.dev
DocumentRoot "/home/gauravdave01/Development/project001/source/public"
<Directory /home/gauravdave01/Development/project001/source/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:8080>
ServerName mno.dev
DocumentRoot "/home/gauravdave01/Development/project002/source/public"
<Directory /home/gauravdave01/Development/project002/source/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Nginx Virtual Host [/etc/nginx/sites-available/default]
server {
listen 80 default_server;
root /usr/share/nginx/example.com;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include snippets/fastcgi-php.conf;
}
}
server {
root /home/gauravdave01/Development/sample.org;
index index.php index.html index.htm;
server_name sample.org www.sample.org;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include snippets/fastcgi-php.conf;
}
}
Once you update all the site information, you need to create a symbolic link to sites-enabled using sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/ then, reload your nginx using: sudo service nginx restart
Don't forget to add newly created site address in hosts [/etc/hosts] file.
If you're getting error which says "File Not Found." when you try to execute .php file, then it means that you need to change the user and group of php-fpm [/etc/php/7.0/fpm/pool.d/www.conf] file to your current user, and the restart php-fpm using: sudo /etc/init.d/php7.0-fpm restart
File References:
Nginx Config: /etc/nginx/nginx.conf
Nginx Error Log: /var/log/nginx/error.log

nginx wont restart after adding www subdomain

I followed tutorial http://wpmu.org/wordpress-multisite-wordpress-nginx and try to add the www subdomain to route to the same location as the domain in my config file.
Only problem, after editing, nginx wont restart...if i delete www.example.com it works perfectly.
Some help would be appreciated!
My config file:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/wordpress;
access_log /var/log/nginx/example.com-access.log;
error_log /var/log/nginx/example.com-error.log;
include conf.d/restrictions.conf;
include /var/www/wordpress/nginx.conf;
include conf.d/wordpress.conf;
}

Resources