I move my Friendly URL's from Apache to nginx and I have a problem. I want to Friendly URL's only works within the subdirectory sgforum.
In PHP, I receive the addresses as: 127.0.0.1/sgforum/index, 127.0.0.1/sgforum/member etc.
When I go on 127.0.0.1/sgforum/ - it works, but when I give member (127.0.0.1/sgforum/member), or index, it downloads a file to my computer, instead of opening with php.
This is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
#listen [::]:80 default_server;
root /home/ariel/workspace;
index index.php index.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# FRIENDLY URLS
location /sgforum/ {
if (!-e $request_filename){
rewrite ^/sgforum/(.*)$ /sgforum/index.php break;
}
}
location ~ /\.ht {
deny all;
}
}
I changed it, and finally works as it should.
# FRIENDLY URLS
location /sgforum/ {
try_files $uri $uri/ /sgforum/index.php;
}
you have to set location for member folder
try change
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;
}
Related
I have my PHP project that handles all the request in index.php file. So, I install nginx on my ubuntu 22.04 and my configuration is:
server {
listen 80;
server_name crm.localtest.me;
root /home/truong/Documents/github/web_projects/crm_system/public;
error_log /home/truong/Documents/github/web_projects/crm_system/public/error_log.html;
location /images {
try_files $uri $uri/ =404;
}
location /js {
try_files $uri $uri/ =404;
}
location /css {
try_files $uri $uri/ =404;
}
location / {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_param SCRIPT_NAME index.php;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
# fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
# }
location ~ /\.ht {
deny all;
}
}
I can browse my home page when access crm.localtest.me but other uri such as crm.localtest.me/auth/login turn to 404 Not Found.
I tried to use
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
but it still not works.
Since I'm new to DigitalOcean and to Nginx server I don't have an idea what I am doing exactly
Having said that here's issue I'm getting
I've put the CakeApp folder inside /var/www/html folder and also I added an info.php page into webroot folder of my CakeApp to check if I can access it
When I go to http://my_ip/CakeApp it redirects to http://my_ip/CakeApp/login page and give a 404, but when I access the info.php file by going to http://my_ip/CakeApp/info.php it works and returns the PHP info page
here is the sever block file
server {
listen 80;
listen [::]:80;
server_name app.cake.com;
return 301 http://app.cake.com$request_uri;
root /var/www/html/CakeApp/public/webroot;
index index.php;
location /CakeApp/webroot {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
So I want to get the login page when I go to http://my_ip/CakeApp
Found a fix.
server {
listen 80;
root /var/www/html/CakeApp/webroot;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
rewrite ^(.+)$ $1 break;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
/etc/nginx/conf.d/default.conf
server{
listen 80;
listen [::]:80;
server_name 192.168.56.101 192.168.101.100 localhost;
root /var/www/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
my codeigniter folder is 'ci' which is located in /var/www/html/ci
what configuration do I need to work url rewriting?...
I didn't want to change the current document root (/var/www/html)
since my 'ci' folder is located at /var/www/html/ci.
So instead, I created a new location block in /etc/nginx/conf.d/default.conf:
server{
...
location /ci {
try_files $uri $uri/ /ci/index.php?/$request_uri;
}
...
}
Thanks to Mert Öksüz for suggesting to use try_files $uri $uri/ /ci/index.php?/$request_uri;.
This one also work:
location /ci {
try_files $uri $uri/ /ci/index.php?$query_string;
}
Change your root to root /var/www/html/ci
Change your try_files to try_files $uri $uri/ /index.php?/$request_uri;
Be sure your fpm path (unix:/var/run/php-fpm/php-fpm.sock;) is correct.
I faced same problem and modified a little bit nginx conf from this site https://gist.github.com/yidas/30a611449992b0fac173267951e5f17f
server {
listen 80;
# For https
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server ipv6only=on;
# ssl_certificate /etc/nginx/ssl/default.crt;
# ssl_certificate_key /etc/nginx/ssl/default.key;
server_name sc.hr;
root /var/www/sc/hr/;
index index.php index.html index.htm;
# set expiration of assets to MAX for caching
#location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {
# expires max;
# log_not_found off;
#}
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass php-upstream;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
# Deny for accessing .htaccess files for Nginx
location ~ /\.ht {
deny all;
}
# Deny for accessing codes
location ~ ^/(application|system|tests)/ {
return 403;
}
}
This conf worked on my laradock nginx container.
This worked for me
location ~* \.php$ {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include fastcgi.conf;
}
In case someone looking for CI 4 nginx on ubuntu 18.04 configuration :
root /var/www/ci/public;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
# this is not working for the first get argument :
# try_files $uri $uri/ /index.php?/$request_uri;
# use this :
try_files $uri $uri/ /index.php$is_args$args;
}
I want to be able to convert the following link:
http://domain.com/foo/bar
into http://domain.com/index.php?controller=foo&action=bar
using php5-fpm. I also want to be able to access static files inside www/ folder. How do I do that? This is what I have so far:
server {
charset utf-8;
client_max_body_size 8M;
listen 80; ## listen for ipv4
server_name domain.com;
root /var/www/domain.com/www;
index index.php;
access_log /var/log/nginx/domain.com.access.log;
error_log /var/log/nginx/domain.com.error.log;
location / {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.(ht|svn|git) {
deny all;
}
}
But it gives me blank page and no get parameters. How should I do this?
The try_files directive is useful when serving static files, if they exist, and rewriting the URI if they do not. See this document for details.
There are a number of ways to achieve this, for example:
location / {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /index.php?controller=$1&action=$2 last;
return 404;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
I'm trying to configure nginx to serve 2 different php scripts from 2 different location. The configuration is as follows.
I have a Laravel installation which resides in /home/hamed/laravel in which its public directory should be served.
I have a Wordpress installation in /home/hamed/www/blog.
And this is my nginx configuration:
server {
listen 443 ssl;
server_name example.com www.example.com;
#root /home/hamed/laravel/public;
index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location /blog {
root /home/hamed/www/blog;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
}
location / {
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
The problem is when trying to access the wordpress section by calling example.com/blog still the the laravel installtion takes over the request.
Now I have tried replacing root directive inside location blocks with alias to no avail.
According to this guide having the index directive or try_files inside location triggers an internal redirect which I suspect causes this behavior.
Would someone please help me figure this out?
The problem is that location ~ \.php$ { ... } is responsible for handling all of your php scripts, which are divided across two different roots.
One approach is to use a common root for the server container and perform internal rewrites within each prefix location block. Something like:
location /blog {
rewrite ^(.*\.php)$ /www$1 last;
...
}
location / {
rewrite ^(.*\.php)$ /laravel/public$1 last;
...
}
location ~ \.php$ {
internal;
root /home/hamed;
...
}
The above should work (but I have not tested it with your scenario).
The second approach is to use nested location blocks. The location ~ \.php$ { ... } block is then replicated in each application's location block. Something like:
location /blog {
root /home/hamed/www;
...
location ~ \.php$ {
...
}
}
location / {
root /home/hamed/laravel/public;
...
location ~ \.php$ {
...
}
}
Now that one has been tested to work.
Thanks to #RichardSmith I finally managed to create the right configuration. Here is the final working config. I had to use the combination of nested location blocks and an inverse regex match for it to work.
server {
listen 443 ssl;
server_name example.com;
root /home/hamed/laravel/public;
# index index.html index.htm index.php;
ssl_certificate /root/hamed/ssl.crt;
ssl_certificate_key /root/hamed/ssl.key;
location ~ ^/blog(.*)$ {
index index.php;
root /home/hamed/www/;
try_files $uri $uri/ /blog/index.php?do=$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
location ~ ^((?!\/blog).)*$ { #this regex is to match anything but `/blog`
index index.php;
root /home/hamed/laravel/public;
try_files $uri $uri/ /index.php?$request_uri;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.hamed.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}