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.
Related
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!
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.
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.
I'm migrating a Kohana App version 3.0.4 from Apache to NginX.
I'm getting troubles with the configuration .htaccess. On NginX work correctly only the internal URL's like:
http://myurlwithoutwww.net.ds/internalpage/anotherpage
but nether the homepage
http://myurlwithoutwww.net.ds/
nether this:
http://myurlwithoutwww.net.ds/index.php
works. The last two will simply display a blank screen.
this is the htaccess file from where I'm trying to migrate:
AddType text/x-component .htc
#Accept-Encoding: filter,deflate
# Turn on URL rewriting
RewriteEngine On
AddDefaultCharset UTF-8
# Installation directory
RewriteBase /
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+).([a-z]{2,3})$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# ExpiresActive On
# <FilesMatch \.(bmp|png|gif|jpe?g|html?|ico|doc|swf)$>
# ExpiresDefault "access plus 10 days"
# </FilesMatch>
FileETag none
# Protect application and system files from being viewed
RewriteRule ^async/?(.*)? index.php?dispatcher=async&$1 [NC,L,QSA]
RewriteRule ^(?:_application|_modules|_system)\b index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
#RewriteRule .* index.php/$0 [PT]
And this my current Nginx configuration:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
include /etc/nginx/aliases.conf;
root /var/www/webroot/ROOT;
index index.php;
location / {
expires off;
try_files $uri $uri/ #kohana;
}
# Prevent access to hidden files
location ~ /\. {
deny all;
}
location #kohana {
rewrite ^/(.+)$ /index.php$request_uri last;
}
location ~* \.php {
#location ~ /\. { deny all; access_log off; log_not_found off; }
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME /var/www/webroot/ROOT/$fastcgi_script_name;
fastcgi_param KOHANA_ENV development;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT /var/www/webroot/ROOT;
fastcgi_index index.php;
}
try below config, also check bootstrap init if it has 'index_file' => FALSE.
server {
listen 80;
server_name *.domain.com;
client_max_body_size 20M;
access_log /home/user/public_html/site/log/access.log;
error_log /home/user/public_html/site/log/error.log;
root /home/user/public_html/site/public/;
index index.php;
location / {
# don’t check $uri/, send to php for nice error message
try_files $uri /index.php?$query_string;
}
location = /index.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
}
It is pretty old question, but maybe you will find it useful, the way I do:
location / {
index index.html index.htm index.php;
try_files $uri index.php;
}
location /kohana {
rewrite ^(.+)$ /kohana/index.php?kohana_uri=$1 last;
if (-f $request_filename) {
break;
}
if (!-e $request_filename) {
rewrite ^/kohana/(.+)$ /kohana/index.php?kohana_uri=$1 last;
}
}
I just listed the main sections of default.conf.
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();