I have an webapp that uses FastCGI. This app is deployed as different "sites". I have a general nginx configuration for the app, and then, I include upstrams and locations files for each site. Each site needs some .js and .css files. Right now, I have set these via the root of the general configuration file. My requirment now, is that I want to be able to use different .css and .js for different sites. So the lookup of the files, should first start at the site/location level and if not found, then search in the general root.
The general nginx conf file is:
# Change this directory depending on the server
include /etc/nginx/conf.d/app/upstreams/*.conf;
server
{
server_name 24.39.17.76;
root /var/www/app/files/;
listen 443;
ssl on;
ssl_certificate /etc/nginx/conf.d/app/ssl/new/app.pem;
ssl_certificate_key /etc/nginx/conf.d/app/ssl/new/app.com.key;
location ~* ^.+\.(css|js|jpg|jpeg|gif|png|ico|svg|woff|ttf)$ {
access_log off; # this is because otherwise, with jail2ban the user may be banned
# add_header Access-Control-Allow-Origin "*";
expires max;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
add_header Pragma public;
}
# Change this directory depending on the server
include /etc/nginx/conf.d/app/locations/*.conf;
}
Note the lines where I include all locations conf and upstreams files.
Then a particular site, has a upstream file like:
upstream siteX
{
server localhost:9021;
server localhost:9022;
server localhost:9023;
}
And a location file like this one:
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}
The html of my webpage that includes the css and js is like this:
<script type="text/javascript" src="/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/TBSDevelopmentLibrary/css/bootstrap.css"/>
But I can easily add a prefix if that helps with any possible solution, like:
<script type="text/javascript" src="/siteX/webfiles/JQDevelopmentLibrary/jQuery.js"></script>
<link rel="stylesheet" type="text/css" href="/siteX/webfiles/TBSDevelopmentLibrary/css/bootstrap.css"/>
SO...the general files are under /var/www/app/files/. However, I would like that for siteX, the files are first searched in another path, say /var/www/siteX/files and only if not found, searched in the general one.
One solution I tried but didn't work is to define this try_files expression:
try_files /siteX/webfiles$uri $uri #fastcgiSiteX;
And then, I put all the folders and files in $document_root/siteX/webfiles, that is, /var/www/app/files/siteX/webfiles/. I thought this solution should work, but it didn't.
Any ideas?
Thanks in advance,
its kind of a hack but it can be done.
location ~ ^/siteX/reps {
access_log /var/log/nginx/app/siteX_reps_access.log;
error_log /var/log/nginx/app/siteX_reps_error info;
if ($arg_service = "true") {
rewrite ^/(.*)$ /service/$1 last;
}
rewrite ^/siteX/(.*)$ /$1 break;
try_files $uri #hack;
}
location #hack {
root /var/www/siteX/files;
try_files $uri $uri/ #fastcgiSiteX;
}
location #fastcgiSiteX {
include fastcgi_params;
fastcgi_param REQUEST_URI $uri?$args;
fastcgi_pass fastcgiSiteX;
fastcgi_next_upstream error invalid_header http_500;
}
Related
I'm tring to access the bundles directory (which is located in : /usr/src/app/public/bundles) of my symfony project.
But, all files in public dir can't be accessed with my navigator. eg:
Request URL: http://localhost:8080/bundles/easyadmin/app.css
Request Method: GET
Status Code: 404 Not Found
The file exists...
This is my nginx config :
server {
server_name ~.*;
location / {
root /usr/src/app;
try_files $uri /index.php$is_args$args;
}
client_max_body_size 100m;
location ~ ^/index\.php(/|$) {
if ($request_method ~* "(GET|POST|PATCH|DELETE)") {
add_header "Access-Control-Allow-Origin" '*' always;
}
# Preflighted requests
if ($request_method = OPTIONS ) {
add_header "Access-Control-Allow-Origin" '*' always;
add_header "Access-Control-Allow-Methods" "GET, POST, PATCH, DELETE, OPTIONS, HEAD";
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
return 200;
}
fastcgi_pass php:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /usr/src/app/public/index.php;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/project_error.log;
access_log /var/log/nginx/project_access.log;
}
I don't know what is missconfigured...
Without seeing any error log I can just guess the issue but lets check directories you are using.
First of all try to avoid root in locations.
Putting root inside of a location block will work and it’s perfectly valid. What’s wrong is when you start adding location blocks. If you add a root to every location block then a location block that isn’t matched will have no root. Therefore, it is important that a root directive occur prior to your location blocks, which can then override this directive if they need to.
The configuration should look like this:
root /usr/src/app/public/;
location / {
try_files $uri /index.php$is_args$args;
}
Your location is missing the public directory. So why do see the 404:
For this request http://localhost:8080/bundles/easyadmin/app.css with your configuration NGINX will look into /usr/src/app/ for /bundles/easyadmin/app.css. And it will not be able to find it. But /usr/src/app/public/bundles/easyadmin/app.css` will be a valid path and should result in 200 OK.
I have problems matching my requirements:
I want 2 things;
https://www.test-boutique.vm/store.css to be routed to the PHP application because the file content is streamed by the app;
https://www.test-boutique.vm/static/css/basic.css to be served from the filesystem because it exists on it;
My vhost is :
server {
listen 443;
server_name www.test-boutique.vm;
root /srv/app/public/front;
index index.php;
location / {
# try to serve file directly, fallback to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
# css are for the files generated by the application (store.css)
location ~ \.(php|htm|css)$ {
try_files $uri $uri/ /index.php$is_args$args;
fastcgi_pass unix:/var/run/php-fpm.app.sock;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param HTTPS on;
fastcgi_param APP_ENV dev;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
try_files $uri =404;
log_not_found off;
access_log off;
expires 7d;
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
}
rewrite ^/media/(.*)$ https://test.cloud/$http_host/media/$1 permanent;
rewrite ^/img/(.*)$ https://test.cloud/$http_host/img/$1 permanent;
access_log /var/log/nginx/fov4_access_log;
error_log /var/log/nginx/fov4_error_log;
}
With this version:
✅ /store.css file works well (generated by the PHP application)
❌ /static/css/basic.css is trying to be served by the PHP application instead of serving the file directly from the filesystem (the file exists for sure under this path)
When removing the css part from the vhost
(location ~ \.(php|htm|css)$ { TO NEW location ~ \.(php|htm)$ {
❌ /store.css file is trying to be served as a static asset and ends up 404 (the request is not passed to the application)
✅ /static/css/basic.css is served correctly
What am I missing please ?
Instead of matching all css files like you do here: location ~ \.(php|htm|css)$ {, try matching that one css file that you need to have generated by PHP:
location ~ \.(php|htm)$ {
# you php-fpm config here
}
location ~* \.(js|css|bmp|png|jpg|jpeg|gif|swf|ico)$ {
# your static files config here
}
location = /store.css {
# you php-fpm config here
}
"I'm setting up an Nginx server on Hostwinds for CI project which is in a subdirectory. Where I want to make a clean URL by removing index.php. Initially, this project was running on Apache server and with the help of .htaccess file, I have made a clean URL by removing index.php. But .htaccess file not works on the Nginx server. So, tell me what codes should I use to remove index.php from URL in 'Hostwinds' Server.
In subdirectory home page of projects opens but when you click on any of its links it will redirects you to 404 page.
I have tried various solution which is available on the internet but none of them worked for me. I have used this code inside nginx.conf file.
some of them:-
1)
location /category/subcategory {
try_files $uri $uri/ /category/subcategory/index.php;
}
2)
location /subfoldername/ {
root /usr/share/nginx/www/subfoldername;
try_files $uri $uri/ /index.php?$query_string;
}
3)
location /api/ {
alias /var/www/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass backend;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
4)
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ /index.php$is_args$args;
}
...http {...
server {
listen 443 ssl http2;
#listen [::]:443 ssl http2 ipv6only=off;
server_name example.com;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ #backend;
}
location #backend {
include proxy_params_common;
# === MICRO CACHING ===
# Comment the following line to disable 1 second micro-caching for dynamic
HTML content
include proxy_params_dynamic;
}
# Enable browser cache for static content files (TTL is 1 hour)
location ~* \.(?:json|xml|rss|atom)$ {
include proxy_params_common;
include proxy_params_static;
expires 1h;
}
# Enable browser cache for CSS / JS (TTL is 30 days)
location ~* \.(?:css|js)$ {
include proxy_params_common;
include proxy_params_static;
expires 30d;
}
# Enable browser cache for images (TTL is 60 days)
location ~* \.(?:ico|jpg|jpeg|gif|png|webp)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for archives, documents & media files (TTL is 60 days)
location ~* \.
(?:3gp|7z|avi|bmp|bz2|csv|divx|doc|docx|eot|exe|flac|flv|gz|less|mid|midi|mka|mkv|mov|mp3|mp4|mpeg|mpg|odp|ods|odt|ogg|ogm|ogv|opus|pdf|ppt|pptx|rar|rtf|swf|tar|tbz|tgz|tiff|txz|wav|webm|wma|wmv|xls|xlsx|xz|zip)$ {
set $CACHE_BYPASS_FOR_STATIC 1;
include proxy_params_common;
include proxy_params_static;
expires 60d;
}
# Enable browser cache for fonts & fix #font-face cross-domain restriction (TTL is 60 days)
location ~* \.(eot|ttf|otf|woff|woff2|svg|svgz)$ {
include proxy_params_common;
include proxy_params_static;
expires 60d;
#add_header Access-Control-Allow-Origin *;
}
# Prevent logging of favicon and robot request errors
location = /favicon.ico {
include proxy_params_common;
include proxy_params_static;
expires 60d;
log_not_found off;
}
location = /robots.txt {
include proxy_params_common;
include proxy_params_static;
expires 1d;
log_not_found off;
}
# Deny access to files like .htaccess or .htpasswd
location ~ /\.ht {
deny all;
}
}
Initially link:- www.example.com/index.php/search-result
resulted in link:- www.example.com/search-result
My forum is installed on the url: example.com/forums
I have used nginx and Vanilla to "prettify" the urls. I've set
/forum/conf/config.php, “RewriteUrls” to “True”.
and in my nginx.conf:
location /forums {
index index.php index.htm index.html;
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 30d;
}
try_files $uri $uri/ #forums;
}
location #forums {
rewrite ^/forums(.+)$ /forums/index.php?p=$1 last;
}
The problem is I installed the sitemap plugin by Vanilla Forums.
and the resulting sitemap is supposed to be located at
example.com/forums/sitemapindex.xml
But when I navigate there nginx gives me a 404.
How do I solve this?
The problem is that the URI /forums/sitemapindex.xml is being processed by the location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ block and not being forwarded to /forums/index.php.
If you do not serve static .xml files, you could simply remove the |xml term from the regular expression.
Otherwise, you will need to make that URI a special case, for example:
location = /forums/sitemapindex.xml {
rewrite ^ /forums/index.php?p=/sitemapindex.xml last;
}
I have a script that increments a global revision number with each deployment of my website. This number is then mapped into the HTML that loads CSS, JavaScript and sprite assets. This is used as a cache-busting strategy.
e.g <link rel="stylesheet" href="/css/screen_r123.css" type="text/css" />
In Apache, I would rewrite these incrementing URLs back to the actual assets like this:
RewriteRule ^css/screen_r(.*).css$ /css/screen_min.css [L]
How would I do the same in nginx? I'm not sure where to place the regex matching logic.
Note: I don't want to append a query ?r=123 to the end of the URI because it feels incorrect to pass a query to a static asset, plus I'm behind a Varnish proxy that doesn't include queries in its cache hashes
Here is my current nginx conf for my site:
server {
listen 8080;
server_name domain.com www.domain.com
port_in_redirect off;
root /usr/share/nginx/mydomain.com/public;
index index.html index.php;
#set long expiry for assets
location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
expires max;
log_not_found off;
}
location / {
#FOLLOWING LINE DOES NOT WORK AS INTENDED
rewrite ^/css/screen_r(.*).css$ /css/screen.css last;
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~* \.php$ {
fastcgi_pass unix:/tmp/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_param APPLICATION_ENV "production";
fastcgi_split_path_info ^(.+.php)(.*)$;
}
add_header "X-UA-Compatible" "IE=Edge,chrome=1";
add_header Cache-Control "public max-age=60";
}
SOLVED. Rewrite directives need to be outside of any location block.
This works:
server {
listen 8080;
server_name domain.com www.domain.com;
port_in_redirect off;
rewrite ^/css/screen_r(.*).css$ /css/screen.css last;
...