I have some trouble getting my nginx setup work the way I want. I have a site example.localhost that is located at /vagrant/frontend/www. My configuration for this, which is working, looks like this:
server {
listen 80;
server_name example.localhost;
root /vagrant/frontend/www;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
}
But then I want to add an admin site, located at /vagrant/backend/www, to the address example.localhost/admin. This is when things go wrong, and I get a 404 not found with the setup below, based on this post:
location /admin {
root /vagrant/backend/www;
}
location ~ /admin/.+\.php$ {
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
The error log looks like this:
2014/04/23 12:30:10 [error] 15459#0: *1 "/vagrant/backend/www/admin/index.php" is not found (2: No such file or directory), client: 192.168.56.1, server: example.localhost, request: "GET /admin/ HTTP/1.1", host: "example.localhost"
I see why I get a 404, but how do I get this right? Any help is much appreciated!
UPDATE
I have changed my admin location to this:
location /admin {
alias /vagrant/backend/www/;
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include /etc/nginx/fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
I have also removed this block:
location ~ /admin/.+\.php$ {...}
Now a call to example.localhost/admin gets processed correct, but if I change it to eg example.localhost/admin/site/index, the request is processed by the frontend. It looks like the /admin location doesn't match... Any thoughts?
location blocks are completely independent. Your request ends up in last location block and never see root directive from first one. Put root directive to second block.
location ~ /admin/.+\.php$ {
root /vagrant/backend/www;
include /etc/nginx/fastcgi.conf;
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
}
server {
listen 80;
server_name example.localhost;
root /vagrant/frontend/www;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
include fastcgi.conf;
}
}
location /admin {
return 301 /admin/;
}
location /admin/ {
alias /vagrant/backend/www/;
try_files $uri $uri/ #handler;
location ~ \.php$ {
include fastcgi.conf;
}
}
location #handler {
rewrite / /admin/index.php?$args;
}
}
Related
I want to set a different php variable on different location directive.
But it is not working.
Here is the code:
server {
listen 1001;
listen [::]:1001;
server_name localhost;
error_log /home/ankurgupta/Desktop/mediaserver.error.log warn;
location /media {
root /home/xxxxxx/Server/websites/localhost/proxy;
index index.php;
try_files $uri $uri/ /media-php/index.php?$query_string;
location ~ \.php$ {
fastcgi_param WEBP "DISABLED";
include nginxconfig.io/php8.1_fastcgi.conf;
}
}
location /media-webp {
root /home/xxxxxx/Server/websites/localhost/proxy;
index index.php;
try_files $uri $uri/ /media-php/index.php?$query_string;
location ~ \.php$ {
fastcgi_param WEBP "ENABLED";
include nginxconfig.io/php8.1_fastcgi.conf;
}
}
}
I'm trying to rewriting /assets/* -> to /theme/theme_1/*.
The rewrite url works with all files except .php-files.
Example file structure:
/theme/theme_1/images/image.jpg
/theme/theme_1/images/user.jpg
/theme/theme_1/ajax/register.php
/theme/theme_1/ajax/read.php
The problem is the PHP-files, I get a 404 with this url:
wget http://example.com/assets/ajax/read.php.
File is found (200) using full path http://example.com/theme/theme_1/ajax/read.php
All other file works fine (200):
wget http://example.com/assets/images/image.jpg
nginx config:
server {
listen 80 default_server;
root /var/www/html;
index index.php index.html
server_name mysite.com;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location /assets {
rewrite ^/assets/(.*) /theme/theme_1/$1 break;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
}
Okie you should try this
location /assets/ {
alias /var/www/html/theme/theme_1/;
}
If that doesn't work then try
location /assets/ {
alias /var/www/html/theme/theme_1/;
try_files $uri $uri/ /index.php?$args;
}
Edit-1
On second look I realize the previous answer won't work as ~ \.php { block will catch everything with php extension and the other assets block can never get called. So the solution is to nest the rewrite inside the php block. So use
location ~ \.php$ {
rewrite ^/assets/(.*)$ /theme/theme_1/$1;
include snippets/fastcgi-php.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I'm trying to make a website with codeigniter in the backend and angularjs in the frontend.
For the backend I'm using nginx as a server.
Currently I have problems with proper configuration of the nginx. What I want to achieve is to have codeigniter and angularjs applications in separate folders. And What I want is to access as follows
codeigniter from my_website.com/api
angular from my_website.com
And in terms of folder I want to keep them in:
codeigniter: /var/www/html/api
angular: /var/www/html/angular
So far I've manage to make it work from the same folder. So now I have codeigniter in /var/www/html and angular folder is in the same directory. And that way I can access codeigniter from my_website.com/ and angular from my_website.com/app which is not what I want.
I was trying multiple different setups for nginx /etc/nginx/sites-available/default file, but every single time with some problems. Farthest I've got was to have those in separate folders but codeigniter was working only for default_controller and I couldn't access any other files.
That's the current working config.
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /var/www/html;
server_name localhost;
index index.php index.htm index.html;
location /app/ {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location / {
try_files $uri $uri/ /index.php;
#index index.html;
}
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/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
include fastcgi_params;
}
}
And here is simple config where I just try to run codeigniter from demo sub-folder,
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.php;
location / {
#try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$request_uri;
location = /demo/index.php {
try_files $uri $uri/ /index.php$request_uri;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/demo$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ \.php$ {
return 444;
}
}
And here my_website.com/demo/index.php works fine but my_website.com/demo/index.php/welcome shows 500 Internal Server Error
I hope I make myself clear.
I tried so many different configs found online, but I always had some troubles with that. Can any of you propose some simple solution for that problem?
Thanks,
Mateusz
I've finally managed to make it work the way I want. I've used this solution link to make it work with codeigniter in sub-directory which was the main problem. Here is my config:
server {
listen 80;
server_name localhost;
root /var/www/html;
autoindex on;
index index.html;
location / {
root /var/www/html/angular;
try_files $uri/ $uri index.html =404;
}
location /api/ {
alias /var/www/html/api/;
try_files $uri $uri/ /api/index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
location ~ \.php$ {
return 444;
}
}
Thanks to #FrédéricHenri for suggesting using the log file under /var/log/nginx/*.log, that helped with tracking the way nginx is redirecting requests. Also what I've encounter was that web-browser was caching the website(I think that is what was happening) so that was giving me false results while I was making some changes to the config file.
If you have already set up ~ .php and other stuff. then Just need to add
location /ci-project {
try_files $uri $uri/ /ci-project/index.php;
}
In your server {} object of Nginx configuration
I have my nginx home at /opt/nginx, inside there are site1 and mail folders, site1 has html folder that is a wordpress installation and mail is a webmail site, both of them must be proxied to php-fpm, site1/html works like a charm no problem at all.
I have the domain1.com and my server delivers site1/html content when domain1.com is requested.
What I want to do is, when domain1.com/mail is requested, serve the content of mail folder (sibling of site1). If I left a index.html file inside mail, when domail1.com/mail is requested, index.html is served to the client without problem but if I try to deliver mail/index.php, 404 error rise instead, what am I doing wrong? below my config:
/etc/nginx/conf.d/domain1.com.conf
server {
.
.
.
root /opt/nginx/site1/html;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /mail {
root /opt/nginx/;
try_files $uri $uri/mail mail/index.php;
}
location ~ [^/]\.php(/|$) {
# SECURITY : Zero day Exploit Protection
try_files $uri =404;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
You can use nested location blocks to invoke PHP scripts which are in a different document root.
Like this:
root /opt/nginx/site1/html;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ^~ /mail {
root /opt/nginx;
try_files $uri $uri/ /mail/index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm.sock;
include fastcgi_params;
}
Notice the ^~ modifier which allows the nested PHP block to take precedence over the outer PHP block. I also removed the path info code which was not being used.
im trying to get the zabbix-frontend to work with nginx.
this here is my nginx conf:
server {
listen 80;
server_name localhost;
root /var/www/test/htdocs;
index index.php index.html index.htm;
location /zabbix {
alias /usr/share/zabbix;
index index.php;
error_page 403 404 502 503 504 /zabbix/index.php;
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
expires epoch;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ \.(jpg|jpeg|gif|png|ico)$ {
access_log off;
expires 33d;
}
}
location / {
try_files $uri $uri/ /index.php;
include /etc/nginx/proxy_params;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_read_timeout 120;
include /etc/nginx/fastcgi_params;
}
include /etc/nginx/security;
include /etc/nginx/main_rules;
}
the php scripts in /zabbix are working! but files like /usr/share/zabbix/css.css are NOT being served (404). in the error log is this:
2013/07/19 20:23:33 [error] 13583#0: *1 open() "/var/www/test/htdocs/zabbix/css.css" failed (2: No such file or directory), client: xxx, server: localhost, request: "GET /zabbix/css.css HTTP/1.1"
so as we can see, nginx is looking for the file in the main root directory /var/www/test/htdocs/ instead of in the alias directory /usr/share/zabbix/.
why is that so and how can i fix that?
I would try to separate them locations. /zabbix with alias only and ~ ^/zabbix/*.php$ with fastcgi. Both outside the / location with root.