NGINX Subdomains Redirecting to Main - .htaccess

I am pretty new to NGINX and have a background with Apache. My company is deploying a server that hosts multiple websites. Specifically libretime in one webroot, and our corporate website in the default webroot.
I am having a heck of a time trying to configure the subdomain. I have ruled out cloudflare and I have the DNS set correctly but it just wont go.
I have created two configs in sites-available and simlinked them. Each going to the respective webroot. I also have configured both the main domain and subdomain with SSL.
Whenever I try and go to the subdomain "https://radio.example.com" it gets redirected to "https://example.com" and never allows me to go to the subdomain.
Please see the below config files, any help would be appreciated as im getting conflicting info.
server {
listen 443 ssl default_server;
listen 80 default_server;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/example.com.pem;
ssl_certificate_key /etc/ssl/private/example.com.key;
server_name www.example.com example.com;
server_name_in_redirect on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/html/example.com;
if ($scheme = http) {
return 301 https://$server_name$request_uri;
}
index index.php index.html index.htm;
client_max_body_size 512M;
client_body_timeout 300s;
location ~ \.php$ {
fastcgi_buffers 64 4K;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# try_files $uri =404;
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Second Subdomain
server {
listen 80;
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/example.com.pem;
ssl_certificate_key /etc/ssl/private/example.com.key;
server_name radio.example.com;
return 301 $scheme://radio.example.com$request_uri;
server_name_in_redirect on;
access_log /var/log/nginx/libretime.access.log;
error_log /var/log/nginx/libretime.error.log;
root /usr/share/libretime/legacy/public;
index index.php index.html index.htm;
client_max_body_size 512M;
client_body_timeout 300s;
location ~ \.php$ {
fastcgi_buffers 64 4K;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# try_files $uri =404;
try_files $fastcgi_script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/run/libretime-legacy.sock;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ ^/api/(v2|browser) {
include proxy_params;
proxy_redirect off;
proxy_pass http://unix:/run/libretime-api.sock;
}
}
Thanks!

Related

nginx wordpress subdomain redirects to root

I have an issue with a Wordpress installation on a subdomain and seems to be redirecting to the root website. When first accessing the subdomain (blog.example.com), it works fine but the second time, it seems to redirect to the root URL (example.com).
Here is the Nginx code:
server {
listen 80;
#listen [::]:80 default_server ipv6only=on;
root /var/www/html/blog-au;
index index.php index.html index.htm;
server_name blog.example.com;
location / {
# try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please do let me know if you need anymore information from me.
Thanks in advance.

location direct domain.com to www.domain.com on multi site server

My server is running LEMP on Debian Jessie.
I am trying to force "www." to occur. I was able to do this with the following solution found on Stackoverflow.
return 301 http://www.example.com$request_uri;
However, I am running several websites from the server and the issue I am running into is as follows.
When logging into wordpress www.site1.com/wp-admin on one website, it routes me to www.site0.com/wp-admin. I believe this issue has to do with a default_server or hostname issue. Do I need to add all hostnames to /etc/hostname ?
Here is my host file
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
listen 443 ssl default_server;
listen [::]:443 ssl default_server;
root /var/www/site0;
index index.php index.html index.htm;
server_name site0.com;
#return 301 http://www.site0.com$request_uri;
location / {
try_files $uri $uri/ =404;
#try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
server {
listen 80;
listen [::]:80;
# SSL configuration
#listen 443 ssl;
#listen [::]:443 ssl;
root /var/www/site1;
index index.php index.html index.htm;
server_name site1.com;
#return 301 http://www.site1.com$request_uri;
location / {
try_files $uri $uri/ =404;
#try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
#fastcgi_index index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I am thinking part of the issue has to do with:
listen 80 default_server;
listen [::]:80 default_server;
Please help if you can, it will be very much appreciate.
I found a very comprehensive guide here: https://www.digitalocean.com/community/tutorials/how-to-configure-single-and-multiple-wordpress-site-settings-with-nginx
The part you might find relevent is this:
# Rewrite rules for WordPress Multi-site.
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
Seems to be rewriting wordpress requests, specifically /wp-admin to the correct domain.

How to redirect my wordpress site's Domain in Nginx no-www to www

I want to redirect my domain example.com to www.example.com
My /etc/nginx/sites-available/example.com configuration is
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?q=$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
What changes should I do in this code?
I have added this line :
try_files $uri $uri/ /index.php?q=$request_uri;
for wordpress permanent link correction.
You need a separate server block to listen for the non www and 301 it to www like this
server {
listen 80;
server_name netmark.com;
return 301 $scheme://www.netmark.com$request_uri;
}
You can read this tutorial to move a domain to another domain but from what I understand you only want to change the actual domain prefix.

Convert .htaccess rules to nginx.conf

Hello I am trying to covert the following .htaccess lines so that arrowchat can work in my nginx server. The location of my arrowchat folder is like this /usr/share/nginx/html/arrowchat/ where /usr/share/nginx/html/ is the root folder for my website. My VPS has only one website, www.jukpac.com hosted on it. Below is the .htaccess code
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^chatroom ^/../public/chatroom/ [L]
RewriteRule ^cron ^/../public/cron/ [L]
RewriteRule ^debug ^/../public/debug/ [L]
RewriteRule ^list ^/../public/list/ [L]
RewriteRule ^mobile ^/../public/mobile/ [L]
RewriteRule ^popout ^/../public/popout/ [L]
RewriteRule ^video ^/../public/video/ [L]
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(gif|jpg|png|css|swf)$">
Header add "Expires" "Mon, 28 Jul 2014 23:30:00 GMT"
Header add "Cache-Control" "max-age=31536000"
</FilesMatch>
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A604800
ExpiresByType text/css A604800
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType application/x-shockwave-flash A604800
</IfModule>
I tried a few online .htaccess converter but it didn't work can some one please let me know how to fix this?
My current default.conf file for nginx looks like this
#
# The default server
#
server {
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
Ok with the Help of a friend, in Serverfault I managed to get the correct configuration as shown below
server { #redirecting server
listen 80;
server_name jukpac.com;
return 301 http://www.jukpac.com$request_uri;
}
server {
listen 80;
server_name www.jukpac.com;
root /usr/share/nginx/html; #move the root to server level
index index.php # move index to server level
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
try_files $uri $uri/ =404;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png|swf)$ {
expires 30d;
add_header Pragma public;
add_header Cache-Control "public";
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}

How to make Zend Framework 2 work with nginx?

I want to make Zend Framework 2 application to run within Nginx server. I could access the homepage, but I could not access any other modules. I found that Nginx needs a rewrite rule to access the URL such as domain/album. The question is how to convert the default .htaccess rules of Zend Framework 2 to Nginx rewrite rule?
Here are the .htaccess rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
You really do not need any rewrite rules to make Nginx and ZF2 to play nice together. Here is a very simple Nginx configuration which I use:
server {
listen *:80;
server_name DOMAIN;
# Character Set
charset utf-8;
# Logs
access_log /vhosts/DOMAIN/logs/access_log main;
error_log /vhosts/DOMAIN/logs/error_log;
# Directory Indexes
index index.php;
# Document Root
root /vhosts/DOMAIN/public;
# Location
location / {
try_files $uri $uri/ /index.php;
}
# Error Pages
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
# PHP-FPM Support
location ~ \.php$ {
fastcgi_pass unix:/usr/local/etc/php-fpm/DOMAIN.sock;
include fastcgi.conf;
}
# Block access to .htaccess
location ~ \.htaccess {
deny all;
}
}
Of course change the paths to your current setup. Since you did not mention what type of PHP installation you are using I can't help you there because I am currently using PHP-FPM.
Using this very simple setup all my modules are working as expected. For example I could visit http://example.com/some/url OR http://example.com/index.php/some/url
Nginx also has a simple configuration for ZF http://wiki.nginx.org/Zend_Framework#Time_for_nginx
Edit 1 - Added fastcgi_params config
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
To follow up on Diemuzi's answer.
Please remember, that if you want query parameters to be working in your application, you have to pass them along with try_files
Best way i found is to use both $is_args and $args. $is_args sets only a question mark if there is arguments, and $args pass the query arguments.
Replace
try_files $uri $uri/ /index.php;
with
try_files $uri $uri/ /index.php$is_args$args;
Now you can use query params as normally in your ZF2 application for example in a default controller
$query_params = $this->params()->fromQuery();

Resources