Trailing slash/redirection issue NGINX - nginx

I am using Drupal 8 with nginx . I have a multiProject Environment with single domain.
I have 10 Drupal sites. which works as
https://example.com/site1
https://example.com/site2
Each site has its on distinct docker container and everything is running smooth on production. But I noticed A issue with few sites. They give 404 without a trailing slash .Only 4 of them. Rest six automatically appends the slash when I remove them and hit in browser.
The nginx config for all are exactly same.
Here is the nginx config for a site :
server
{
client_max_body_size 128m;
root /var/www/html;
index index.php index.html index.htm;
location /site1 {
try_files $uri/ $uri /site1/index.php?$query_string;
}
error_page 404 /404.html;
error_page 403 /403.html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
}
I have tried every thing , rewrite, try files and returns.
can any one help.

Related

nginx multiple domain issue

I want to host two website on my ubuntu VPS with nginx . so i created two copy of default config :domain1.com and domain2.com
config for domain1.com:
server {
listen 80 ;
listen [::]:80 ;
root /home/sher/ftp/www/public;
server_name domain1.com www.domain1.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php?$query_string;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
if user type in www.domain1.com in browser he would see domain1 site. but if he hit it without www he would see domain2.com .i've created symlinks from these two config files to sites-enabled .
P.S : I set listen to default_server for domain1 and it seems the issue solved but im not sure if it is the right way.

Laravel and WordPress integration routing using nginx

I am developing web app in Laravel 5.2. I have existing WordPress site. So, I want to integrate Laravel with WordPress. WordPress app has static pages. I have two separate directories for Laravel and WordPress in my root directory.
laraApp
wpApp
I want to make wpApp as default app. So when user clicks login button, user will be redirected to laraApp. I want wpApp at www.example.com and laraApp in www.example.com/laraApp. I have nginx web server running. So what should be my nginx config file?
Current nginx config file is :
server {
listen 80;
root /var/www/root/wpApp;
index index.php index.html index.htm;
server_name www.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# rewrite rules for laravel routes
location /laraApp {
rewrite ^/laraApp/(.*)$ /laraApp/public/index.php?$1 last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here my Laravel app is accessible using url www.example.com/laraApp/public/
I want to access it using www.example.com/laraApp.
Thanks.
The configuration would be simpler if the base URI for each of the applications did not overlap. But given the constraints of your question, you must use two distinct document roots for the PHP section of each of the applications in your configuration.
As you have placed one of your applications in /, the other application is kept separate by the use of nested location blocks. Notice the use of the ^~ modifier to prevent the first PHP block from processing Laravel requests.
index index.php index.html index.htm;
root /var/www/root/wpApp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ^~ /laraApp {
rewrite ^/laraApp(.*)$ /laraApp/public$1 last;
}
location ^~ /laraApp/public {
root /var/www/root;
try_files $uri $uri/ /laraApp/public/index.php?$query_string;
location ~ \.php$ {
try_files $uri /laraApp/public/index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
I am away from my test system at the moment so the above has not been syntax checked or tested.

NGINX doesn't work if the domain has an accent mark

I have bought a domain with an accent mark inside: something like www.èxample.com.
My problem is that even if I configured my nginx server block (virtual host) in a correct way, it doesn't match the directory that I made.
I tried to use the same configuration with a domain without an accent mark and everything work fine.
This is my server block file inside site-availables folder:
server {
listen 80;
root /var/www/example.com;
index index.php index.html index.html;
server_name *.èxample.com;
client_max_body_size 4G;
charset utf-8;
access_log /var/www/example.com/logs/nginx-access.log;
error_log /var/www/example.com/logs/nginx-error.log;
location / {
try_files $uri $uri/ /index.html;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
How can I solve this problem?
Thanks!

Custom 404 page not displaying

I just started with Nginx, and did the basic configuration, regarding configuring the PHP FPM, and I've changed the index file to be index.php instead of index.html. I also managed to get a handle on URL rewriting, although omitted from my example below for simplicity.
But in the default configuration file, there is a section dedicated to error pages, which looks as if it's ready to "plug and play". I have uncommented them, but they don't work correctly. I've gone through some 404-related questions on Stackoverflow, everyone seems to recommend that as the correct syntax.
Maybe the order of instructions is wrong, however, this is from the original sample configuration, so I don't know why it wouldn't be working.
I have remove all the commented lines from the configuration file, what's left is this:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
index index.php;
server_name localhost;
location / {
try_files $uri $uri/ index.php;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Now, when I go to http://localhost/this-page-doesnt-exist, the browser displays a literal File not found., instead of the contents of my /404.html file.
Also, the permissions of my 404.html file is the same as the permissions for the index.php, which is 644. The owner is the same as well.
What could be wrong?
try_files is passing your request for a none-existent file to index.php. This is in turn sends the request to php-fpm. By default Nginx returns the response from php-fpm to the client which is what you are seeing.
Update your php-fpm config to look like this and inform Nginx to handle error codes in the response.
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
}
The documentation is here: fastcgi_intercept_errors

Nginx still showing the non existant URL when on the 404 page (index in my case)

I'm having a slight issue with Nginx and I'd be grateful if somebody could help me figure out the reason why it's not working.
What I'd like to do is have the website redirect to the index page when the requested URL is invalid, but when using the following code:
server {
listen 80;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name localhost;
error_page 404 /index.php;
error_page 500 502 503 504 /50x.php;
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri $uri/ =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
it's not working exactly the way I'd like it to. This code works, but the URL is still http://example.com/nonexistantpage when showing the index page. What I'd like is for the site to redirect to the index page, making the URL http://example.com
ok that's easy try this
error_page 404 =301 /;
if it doesn't work then use the full path
error_page 404 =301 http://example.com;

Resources