I have been trying to get this to work for most of the afternoon and have tried several of the solution I have seen online with no luck. I have tried putting the $uri twice, but cant get the try_files to pick up my other images size. Any help would be greatly appreciated (exmaples).
server {
listen 80;
server_name mysite.com;
root /web/mysite.com;
location / {
index index.html index.htm;
}
# directory based redirects
location /mimg/ {
alias /web/mysite.com/images/;
# cache expiration
expires 30d;
# set default error page
set $error_page_404 /samples/_default.jpg;
# failover chain samples (16:9 content flag set)
location ~* ^/samples-304x171/(.*)$ {
set $error_page_404 /samples/_default-16x9.jpg;
}
location ~* ^/samples-400x225/(.*)$ {
try_files $uri /samples-304x171/$1;
}
location ~* ^/samples-640x360/(.*)$ {
try_files $uri /samples-400x225/$1;
}
location ~* ^/samples-800x450/(.*)$ {
try_files $uri /samples-640x360/$1;
}
# set default image
error_page 404 $error_page_404;
} # end of /mimg
}
Related
I have searched many articles and posts but didn't find any particular fix for my requirement. Hence, posting this question.
I have 2 locations on my server /p1 and /p2. The default location should be /p1/index.html.
I want, when I will access http://localhost:8080/p1 or http://localhost:8080/p2 then I should be able to get the data from http://localhost:8080/p1/index.html, and also the URL should change in the browser.
Can we use any other directives instead of location directives to achieve this?
Below is the current Nginx config :
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri /p1/index.html;
}
location /p1 {
try_files $uri /p1/index.html;
}
location /p2 {
try_files $uri /p1/index.html;
}
}
Need in this way :
http://localhost:8080/p1 --> http://localhost:8080/p1/index.html
http://localhost:8080/p2 --> http://localhost:8080/p1/index.html
Any suggestion or help will be grateful.
Got some clue from #BijayRegmi's comment. I have modified little and the below changes worked for me.
server {
listen 80;
root /usr/share/nginx/html;
location / {
try_files $uri /p1/index.html;
}
location /p1 {
try_files $uri /p1/index.html;
}
location /p2 {
return 301 $scheme://$http_host/p1/index.html;
}
}
I am trying to move an app from apache server to nginx. The problem is that there are multiple apps in subdirectories and I can't find a proper way to configure the server.
What I need:
www.example.com serves from /srv/app
www.example.com/sub1 serves from /srv/app/sub1
www.example.com/sub2 serves from /srv/app/sub2
Each of the apps need the same config, so I extracted that in a snippet:
# snippets/app.conf
index index.php index.html index.htm index.nginx-debian.html;
location /system {
return 403;
}
# [a couple of other 403s excluded]
# Pass non-file URI to index.php
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Use PHP
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
# Hide .htaccess
location ~ /\.ht {
deny all;
}
And in the main server file:
# [non-www and http redirects]
server {
# [listen directives]
server_name www.example.com;
root /srv/app;
include snippets/app.conf;
location /sub1 {
root /srv/app/sub1;
include snippets/app.conf;
}
# [other sub-apps included in the same way]
# [ssl stuff]
}
However, this gives me an error:
nginx: [emerg] location "/system" is outside location "/sub1" in /etc/nginx/snippets/app.conf:5
It's obvious from the error that /system is interpreted as being "absolute" www.example.com/system instead of the nested www.example.com/sub1/system. Can I somehow specify that I want the nested locations to be considered relative? Or I just have to repeat the whole near-identical config for every sub-app changing the prefixes?
It turns out that most of the repeating is not necessary in nginx.
The directives to use fastcgi for .php and hide /.ht files were already regexes so they affect everything. It's enough to specify index once and the default there stuff was redundant if I only want to use index.php.
As all the apps are nested on the filesystem in the same way as on web, specifying root was not necessary either.
What surprised me was that location ^~ /(system|data)/ { ... } matches not only www.example.com/system/, but also www.example.com/sub1/system/. I thought that ^~ should match only if the location start matches the regex...
# [non-www and http redirects]
server {
# [listen directives]
server_name www.example.com;
root /srv/app;
index index.php;
location ^~ /(system|data)/ {
return 403;
}
# Use PHP
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
# Pass non-file URI to index.php for all locations
location /sub1/ {
try_files $uri $uri/ /sub1/index.php?$query_string;
}
location /sub2/ {
try_files $uri $uri/ /sub2/index.php?$query_string;
}
# [other sub-apps included in the same way]
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ /\.ht {
deny all;
}
# [ssl stuff]
}
I also tried to replace the separate locations with
location ^~ /(sub1|sub2)/ {
try_files $uri $uri/ /$1/index.php?$query_string;
}`
but didn't succeed with that - this location somehow didn't ever match and everything got passed to the /index.php in base instead.
The current nginx conf I have looks like:
server {
listen 80;
server_name mydomain.com;
root /home/myname/some_app/public;
location / {
try_files $uri #some_named_location;
}
location /sub {
root /home/myname/other_app/public;
try_files $uri #other_named_location;
}
}
I expect mydomain.com/sub/xxx to be served by /home/myname/other_app/public/sub/xxx, but instead it's served by /home/myname/some_app/public/sub/xxx. What's going wrong here?
I also tried using alias instead of root in the /sub location block:
location /sub {
alias /home/myname/other_app/public;
try_files $uri #other_named_location;
}
Then I expect mydomain.com/sub/xxx to be served by /home/myname/other_app/public/xxx, but still it's served by /home/myname/some_app/public/sub/xxx.
I even tried moving the server block's root directive into the / location block.
server {
location / {
root /home/myname/some_app/public;
try_files $uri #some_named_location;
}
location /sub {
root /home/myname/other_app/public;
try_files $uri #other_named_location;
}
}
But it still doesn't work.
I have a nginx server with the following code added to the sites conf file. The first part is an alias to allow the folder called images to be severed when visiting for example: example.com/images
The second part has been added to allow permalinks in wordpress to work. Problem is each of the code blocks work separately but not together. The offending line of code is:
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
This code stops any files being server from example.com/images and shows a 404 error
location /images {
alias /var/www/clients/client0/web6/images;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
Why does the cache line conflict?
Use try_files. This way you can work with settings in another block.
Example url: http://your-site.com/img/lorena_improta.jpg
root /var/www/html/stackoverflow;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~* ^/img/(.*)$ {
try_files $uri /48725060/images/$1;
}
location ~* \.(jpe?g|gif|png) {
expires 1h;
}
What I'm trying to achieve is if someone visit my home/index page, I need to server my index.html file. But, if it's another URL/path pass the request to my index.php file (I'm using Symfony).
I follow this example, but is not working. It's always serving my PHP file.
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /index.html
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}
I will appreciate any help or guidance you can share with me.
This finally worked for me:
server
{
listen 80;
server_name mysite.com;
root /path/to/my/web;
index index.html index.php;
location = /
{
try_files $uri /index.html?$args;
}
location /
{
try_files $uri /index.php?$args;
}
}