/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;
}
Related
I have configured nginx with multiple locations, one for a laravel project and another for a native php project.
Laravel project is working perfectly, but the second location seems to give:
"403 Forbidden nginx/1.14.0 (Ubuntu) "
Here is my default file:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/washyapi/public;
try_files $uri $uri/ index.php$is_args$args;
index index.html index.htm index.php;
server_name 167.71.239.178;
location / {
try_files $uri $uri/ index.php$is_args$args;
try_files $uri $uri/ /index.php?$query_string;
#index index.php;
}
location /admin {
root /var/www/html/;
#autoindex on;
#autoindex_exact_size off;
index index.php;
#try_files $uri /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
}
after few days of struggling, here what i have done to make it work.
Here's the working configuration to have two apps working, where one application exists in a subdirectory of another.
default file :
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/top/public;
index index.html index.htm index.php;
server_name _;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /nested {
alias /var/www/nested/public;
try_files $uri $uri/ #nested;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
location #nested {
rewrite /nested/(.*)$ /nested/index.php?/$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
don't forget to restart the nginx server :
service nginx restart
I'm in this weird situation, I set up my nginx and everything is working fine until I change the index file of the second host from index.html to index.php. When I make this change the second host show a blank page.
#
# HOST 1
#
server {
listen 80;
listen [::]:80;
server_name domain1.com;
root /var/www/Folder1/public;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
#
# HOST 2
#
server {
listen 80;
listen [::]:80;
server_name domain2.com;
root /var/www/Folder2/public;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Settings are the same but meanwhile for the first are working perfect, for the second no.
Thanks in advance.
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;
}
}
I developed my CI app and tested it on over 4 Apache server and it's working fine. But aftaer i upload it int to the new server which is Nginx, it has problem with urls. I Googled a lot and approximately did every thing but there was not result. Now here's the problem:
Due to project structure and scale i need every url to be like this:
http://example.com/myappname/controller/action
ex: http://example.com/myappname/auth/login
But it's not working. What is working is:
http://example.com/myappname/index.php?/auth/login
which is not what i want.
The nginx configuration is:
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php?/$request_uri;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
}
Your codeIgniter config.php contains the following information:
$config['base_url'] = "http://domain.tld/";
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";
And here is the nginx rewrite rule,
server {
server_name domain.tld;
root /var/www/codeignitor;
index index.html index.php;
# 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 =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I had the same problem.
I have a website in
http://website.com
and the codeigniter folder in
http://website.com/partners.
I just change the nginx configuration to have a location specifically for this codeigniter folder.
I add it like this.
location / {
try_files $uri $uri/ /index.php;
}
location /partners {
try_files $uri $uri/ /partners/index.php;
}
As for your case. I think this setting should work like below :
server {
listen 80;
listen [::]:80;
root /var/nginx/www/mydomain.com/www;
index index.php index.html index.htm;
server_name mydomain.com www.mydomain.com;
location / {
try_files $uri $uri/ /index.php;
}
#ADD THIS LINE
location /myappname {
try_files $uri $uri/ /myappname/index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
# With php5-fpm:
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
location ~* \.(png|jpg|jpeg|gif|ico|ttf|woff)(\?ver=[0-9.]+)?$ {
expires 1w;
}
location ~* \.(css|js|html)(\?ver=[0-9.]+)?$ {
expires 1w;
}
Make sure to test that everything is working by running sudo nginx -t and then restart nginx. Hopefully it will work.
I am trying to set up an NGINx server to work with Phalcon PHP Framework.
So far I've been looking for help on the internet but I could not find anything...
My conf file is:
server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
What should add to it in order to make Phalcon work?
Thank you.
I faced the same problem and finally got this working here is my configuration file using
Windows 7 and PHP 5.3 in Fast CGI mode.
server {
listen 80;
server_name localhost;
set $root_path 'C:/devtools/phalcon/test/public';
root $root_path;
index index.php index.html index.htm;
try_files $uri $uri/ #rewrite;
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location ~ \.php {
try_files $uri =404;
fastcgi_index /index.php;
fastcgi_pass 127.0.0.1:9123; #default is 9000, i am using 9123
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root $root_path;
}
location ~ /\.ht {
deny all;
}
}