Local nginx wildcard installation not loading JS files - nginx

I have installed a nginx server configuration on my localhost. It's a multi store PrestaShop 1.6.1.4. You can see the nginx configuration right here:
server {
listen 80;
listen [::]:80;
root /var/www/html/devsite;
index index.php index.html index.htm;
server_name .devsite.com;
location / {
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
rewrite ^/([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$1$2.jpg last;
rewrite ^/([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$1$2$3.jpg last;
rewrite ^/([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$1$2$3$4.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$1$2$3$4$5.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$1$2$3$4$5$6.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$1$2$3$4$5$6$7.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$1$2$3$4$5$6$7$8.jpg last;
rewrite ^/([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])([0-9])(-[_a-zA-Z0-9-]*)?(-[0-9]+)?/.+\.jpg$ /img/p/$1/$2/$3/$4/$5/$6/$7/$8/$1$2$3$4$5$6$7$8$9.jpg last;
rewrite ^/c/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
rewrite ^/c/([a-zA-Z-]+)(-[0-9]+)?/.+\.jpg$ /img/c/$1.jpg last;
rewrite ^/([0-9]+)(-[_a-zA-Z0-9-]*)(-[0-9]+)?/.+\.jpg$ /img/c/$1$2.jpg last;
try_files $uri $uri/ /index.php?$args;
}
# AlphaImageLoader for IE and fancybox
rewrite ^images_ie/?([^/]+)\.(jpe?g|png|gif)$ js/jquery/plugins/fancybox/images/$1.$2 last;
# Web service API
rewrite ^/api/?(.*)$ /webservice/dispatcher.php?url=$1 last;
# Installation sandbox
rewrite ^(/install(?:-dev)?/sandbox)/(.*) /$1/test.php last;
#Change this block to your admin folder
location /admin {
if (!-e $request_filename) {
rewrite ^/.*$ /admin/index.php last;
}
}
# Source code directories
location ~ ^/(app|bin|cache|classes|config|controllers|docs|localization|override|src|tests|tools|translations|travis-scripts|vendor|var)/ {
deny all;
}
# Prevent exposing other sensitive files
location ~ \.(yml|log|tpl|twig|sass)$ {
deny all;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I have arrived to the conclusion it's some sort of missing configuration with the Nginx Wildcard configuration, but I might be wrong, I'll quickly explain what I can see:
The page loads fine, admin works fine but there's a template that doesn't load the respective JS which enables the customer product selection process.
I have checked the inspector network debugger and I can see the file being loaded in the production environment correctly whereas it doesn't load as in the localhost environment...
Thanks for the help.

Things you may check, that have worked for me, at least once in the past:
Check the permissions in the local environment, i.e. use
namei -nom path/to/jsfile
to check that the path to the resource is accessible and ownership is appropriate (use chmod and chown to make appropriate alterations)
Check the permissions of the file itself (maybe an app or a process rewrites each time you run your service the js and file permissions are reset)
Check if you production instance is not really servicing the js file but has it cached from a previous state. Install maybe a clear cache button/add on or something that will allow you to deactivate cache. Some browsers have this feature.
Depending on your dev environment (is it Django is it something else?) it can be the case that servicing static files in the dev and in the prod environments might differ by design.
Check if the code you have in the dev and in the prod environments are really the same!
Stop the prod environment (if this won't interrupt any real service and won't bother your clients) and restart it. Do you experience the same issue?
Check again if both prod and dev look at the same nginx config file. Restart nginx and reload config
Try removing from dev nginx config the complex regex statements and check if js file can be serviced
Check locations of dev and prod static files and verify that they are proper in your config
As soon as I remember something additional I will post it here. Good luck!

Related

NGNIX url rewriting doesn't use root statement

in the site configuration I have this simple statement:
location /test/mysite/ {
root /folder/;
rewrite ^/test/mysite/(.*)$ /$1 last;
}
I would like that ngnix remove the /test/mysite/ part and accesses the resource from /folder/.
For example, if I go to http://mywebsite/test/mysite/test.html
it would load the file ŧest.html from /folder/test.html

SuiteCRM REST API v8 Not working with Nginx (ubuntu 18.0)

I try to get access token Using REST API v8 but getting 404 . error.
I have change nginx conf file for rewrire rules but it's not working .
location /api {
rewrite ^/api/(.*?)$ /lib/API/public/index.php/$1 break;
}
We faced the same issue with SuiteCRM 7.11.5 configured in Ngnix. Access token cant be available on calling the API. There is some sort of redirections issues faced in suitecrm with nginx. It is to better to update your docker. server with Apache and predefined .htaccess file in directories.
This fixed our issue on 404 error in API response.
This works for me:
location ~ /Api/ {
index index.php;
try_files $uri #rewrite_api;
location ~ \.php {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
location #rewrite_api {
rewrite ^/Api/(.*)?$ /Api/index.php/$1 last;
}
In addition to the response to make it work with access token request Api/access_token
It has to look like
location #rewrite_api {
rewrite ^/Api/access_token(.*)?$ /Api/index.php/$1 last;
rewrite ^/Api/(.*)?$ /Api/index.php/$1 last;
}

phpBB rewrite rules and php parser rules conflicting on nginx

Guys I am so bad at this nginx rewrite rules. Sad thing is that nginx is sooooo lightening fast that I cannot also quit using it.
Please help me.
I have installed SEO urls on the forum.
But i am unable to control the php files and the SEO urls both together.
If i open the SEO urls then php parser stops working and i can download the php source files instead.
/forum/compare-cameras-support-f1/
BUT if i open php URLs in format below they work fine.
/forum/viewforum.php?f=1
I have spent hours trying to make both work but have just given up.
location ^~ /forum/ {
# forum will run separate
# rewrite rules for forum
rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*)-s([0-9]*).html /forum/viewtopic.php?f=$2&t=$4&start=$5&$query_string break;
rewrite ^/(.*)-f([0-9]*)/(.*)-t([0-9]*).html /forum/viewtopic.php?f=$2&t=$4&$query_string break;
rewrite ^/(.*)-f([0-9]*)/index-s([0-9]*).html /forum/viewforum.php?f=$2&start=$3&$query_string break;
rewrite ^/(.*)-f([0-9]*)/ /forum/viewforum.php?f=$2&$query_string break;
rewrite ^/(.*)-f([0-9]*) /forum/viewforum.php?f=$2&$query_string break;
# app rewrite rules for the INSTALLATION
#try_files $uri $uri/ /forum/install/app.php?$query_string;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Please help

Yii2 adanced application and nginx configuration (index directive configuration)

I'm trying to build proper server configuration for nginx serving Yii2 advanced template where backend hosted in a subfolder inside same domain name as frontend.
In this case path_to_yii2 contains whole Yii2 application template and we have these requirements:
path_to_yii2/frontend/web -> should be webroot of / location
path_to_yii2/backend/web -> should be webroot of /backend location
Static content in both folders should be properly served. PHP files should work in both cases.
I wrote and tested such configuration:
server {
listen 80;
server_name localhost;
root <path_to_yii2_application>;
location ~* ^/backend/(.+)$ {
try_files /backend/web/$1 /backend/web/$1/ /backend/index.php?$args;
index /backend/$1/index.php; # not working in case of exact /backend/ request
location ~* ^/backend/(.+\.php)$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/backend/web/$1;
}
}
location / {
try_files /frontend/web/$uri /index.php?$args;
index /$uri/index.php; # not working at all, but / location is served by php even without this line
}
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/frontend/web/$fastcgi_script_name;
}
}
And I have some unresolved problems with such configuration. I tested six different options:
FRONTEND:
Static content under / location should be served directly from frontend/web subfolder of yii2 application folder.
Nonexistent content here should be redirected to frontend/web/index.php?$args and served using ~ .php$ location with fastcgi.
Directories under / location should return indexes, if needed served with ~ .php$ location and fastcgi.
BACKEND:
Static content under /backend location should be served directly from backend/web subfolder of yii2 application folder.
Nonexistent content here should be redirected to backend/web/index.php?$argsand served using ~ .php$ location with fastcgi.
Directories under /backend location should return their indexes, if needed served with ~ .php$ location and fastcgi.
I have troubles with directories and their indexes (3 and 6).
First of all, directories for frontend section not working at all, seems that index /$uri/index.php; is wrong for some reason.
Secondly, directories for backend working except exact /backend/ url. Nginx doesn't serve index directive in =/backend/ case.
As a temporarily workaround for backend I added few lines for this exact url:
location = /backend {
return 301 https://$server_name/backend/index.php;
}
location = /backend/ {
return 301 https://$server_name/backend/index.php;
}
How to fix these indexes correctly and what I'm doing wrong?
P.S. There are some similar questions, like Migrating Yii2 from Apache to Nginx - failed on backend app (doesn't provide correct answer, recommends using subdomain) and Nginnx config for Yii 2 Advanced App Template (suggested to move backend content inside yii2 application to frontend folder). I believe that nginx configuration is a proper way of congiguring yii2-application template.
There is also https://github.com/mickgeek/yii2-advanced-one-domain-config repositary which not works in nginx > 1.8.1.
Interesting that apache just needs a symbolic link to make this work. Nginx before 1.8.1 too.
Yii2 application template can be git cloned from here: https://github.com/yiisoft/yii2-app-advanced.git

WordPress 3.0 & nginx - permalink, 404 problem

I've installed nginx, FastCGI and PHP on my server. WordPress 3.0 installed after a bit of a monster battle, but it's installed and working well.
However, when I change the permalink settings to anything other than default, I get 404 errors on every post, article and page.
I understand that this is something to do with nginx not supporting .htaccess and WordPress getting confused with where to go when a page is requsted.
I've tried a few rewrites in the nginx conf files and even the nginx compatibility plugin; neither have worked. With one rewrite I managed to stop the 404 errors, but instead of WordPress finding the post I was after I merely got my PHP confirmation page. Bah.
Forums are littered with people with similar issues. Does anyone have a solution?
On your location / block,
add this and remove any non-specific rewrite rules:
try_files $uri $uri/ /index.php;
If wordpress is on another directory besides the root, instead of having
if (!-e $request_filename) {
rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
}
You can have:
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
This page has exactly the same concept. I should have read and tried it first: nginx rewrite rule under a subdirectory
After much pain:
# if filename doesn't exist, take the request and pass to wordpress as a paramater
if (!-e $request_filename) {
rewrite ^/wordpress/(.+)$ /wordpress/index.php?q=$1 last;
}
If the requested file does not exist, pass it to index.php. It's a bit slow and I think I might try and not use a query, but it does work... :)
Have you tried the nginx Compatibility plugin?
Plus ElasticDog seems to provide a fairly concise article on getting WP working with nginx - which includes getting pretty permalinks to work.
Here's another article that seems to deal specifically with nginx rewrite rules for WordPress.
This was how I solved my permalinks in my wordpress blogs in dreamhost.
Inside the folder /home/ftpusername/nginx/example.com/ (if you don't have it, create it)
created the file
nginx.conf with the following content
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
restarted the nginx
/etc/init.d/nginx reload
Some notes:
ftpusername and example.com MUST be changed according to your system.
That was it!
Good luck for u all.
this does not work if you are using location other than / like:
~ .php$, what i meant to say that pretty link will work but your graphics will be all over the place. so what you need is exactly stated below.
http://www.pearlin.info
location ~ \.php$
{
try_files $uri $uri/ /index.php?$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;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?url=$1 break;
}
I did the following..
in the folder
/home/userrunningnginx/nginx/domain.com
I have:
default.conf (file)
include /home/neukbaarofnietps/nginx/neukbaarofniet.com/drop;
drop (file)
# Rather than just denying .ht* in the config, why not deny
# access to all .invisible files
location ~ /\. { deny all; access_log off; log_not_found off; }
nginx.conf (file)
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
WORDPRESS-NGINX.CONF (file)
#######################
# WP Super Cache
# if the requested file exists, return it immediately
if (-f $request_filename) {
break;
}
set $supercache_file '';
set $supercache_uri $request_uri;
if ($request_method = POST) {
set $supercache_uri '';
}
# Using pretty permalinks, so bypass the cache for any query string
if ($query_string) {
set $supercache_uri '';
}
if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_" ) {
set $supercache_uri '';
}
# if we haven't bypassed the cache, specify our supercache file
if ($supercache_uri ~ ^(.+)$) {
set $supercache_file /wp-content/cache/supercache/$http_host$1/index.html;
}
# only rewrite to the supercache file if it actually exists
if (-f $document_root$supercache_file) {
rewrite ^(.*)$ $supercache_file break;
}
# all other requests go to Wordpress
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
Adding this block to your nginx.conf should solve the issue:
if (!-e $request_filename) {
rewrite ^/wordpress_dir/(.+)$ /wordpress_dir/index.php?q=$1 last;
}
Hope this helps.
Good luck.

Resources