When I try accessing: https://www.mysite.com, it downloads a php file, however when I access https://www.mysite.com/index.php, there is no problem.
server {
listen 443;
server_name www.mysite.com;
ssl on;
ssl_certificate /etc/nginx/ssl/public.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;
ssl_session_timeout 5m;
root /var/www/mysite.com;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ^~ /app/ { deny all; }
location ^~ /includes/ { deny all; }
location ^~ /lib/ { deny all; }
location ^~ /media/downloadable/ { deny all; }
location ^~ /pkginfo/ { deny all; }
location ^~ /report/config.xml { deny all; }
location ^~ /var/ { deny all; }
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~ \.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;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS on;
}
}
So, php is not interpreted.
I've had similar problems before, where the page would just show the source code for index.php whenever a 404 error occurred.
try_files $uri $uri/ /index.php?$query_string;
Try this:
try_files $uri $uri/ =404;
error_page 404 = /index.php?$query_string;
If the problem still occurs, try this adding this:
location ~ ^/?$ {
try_files $uri $uri/ index.php =404;
}
This is an ugly hack, but it worked for me on my old server. Haven't needed it in my new setup for some reason.
Related
/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 have a little problem.
Now, I'm migrating my WordPress site from Apache2 to Nginx.
Everything works fine, but not admin panel.
When I try to access '/wp-admin' I have the following redirect:
http://domain.com/wp-login.php?redirect_to=http%3A%2F%2Fdomain.com%2Fwp-admin%2F&reauth=1
After that a blank page appears and nothing else.
I have checked PHP configuration and sample test.php is printing something so it works.
What I'm doing wrong?
Here is my Nginx config:
server
{
listen 155.123.12.222:80;
server_name domain.com www.domain.com ;
access_log /var/log/nginx/domains/domain.com.log;
access_log /var/log/nginx/domains/domain.com.bytes bytes;
error_log /var/log/nginx/domains/domain.com.error.log;
root /home/admin/domains/domain.com/public_html;
index index.php index.html index.htm;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /wp-admin {
index index.php index.html index.htm;
try_files $uri /wp-admin/index.php?q=$uri&$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ \.php$
{
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/nginx_limits.conf;
if (-f $request_filename)
{
fastcgi_pass unix:/usr/local/php56/sockets/admin.sock;
}
}
location ~ /\.ht
{
deny all;
}
include /etc/nginx/webapps.conf;
}
Try removing the following lines:
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /wp-admin {
index index.php index.html index.htm;
try_files $uri /wp-admin/index.php?q=$uri&$args;
}
Ok, I found a solution.
I have turned on a DEUB mode in wp-config.php. There was a bad configuration in some plugin, so I changed it.
Anyway, thanks for your attention guys.
How do I configure nginx to allow a slash between my /test_file.php/?param1=test ? Currently is only allowing /test_file.php?param1=test ...
Here is my current configuration:
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
# rewrite ^/(.php*)/$ /$1 permanent;
root /var/www/example.com;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/example.com-error.log error;
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Url working (undesirable):https://example.com/workouts.php?workout=206
Url I want: https://example.com/workouts.php/?workout=206
The block:
location ~ \.php$ { ... }
is responsible for processing any URI which ends with .php.
A simple solution would be to change the regular expression to accept URIs which include pathinfo. However, you should also make other changes within the block to mitigate known exploits. See this document for details.
For example:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
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've installed nginx and I try to run wordpress on it.
Everything works fine, except for permalinks.
Here is the vhost-file I'm using:
server {
listen 123456:80;
server_name my-domain.com;
if ($host ~* www\.(.*)) {
set $wwwless $1;
rewrite ^(.*)$ $scheme://$wwwless$1 permanent;
}
root /var/www/my-folder;
index index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
(I've replaced critical data in the above code with my-domain.com, my-folder and the ip 123456.)
index.php, the admin-panel and using the standard links (.../?p=123) work finde. If I enable some of the permalinks, index.php and the admin-panel still work. But if I try to open another site of the wordpress blog, my browser downloads the index.php :(
Try something like this for the .php location config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Here's my wordpress config modified to suit you.
server { # the redirecting from www to non-www
server_name www.example.com;
return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
listen 80;
server_name example.com;
root /path/to/root;
# make sure the directory /var/log/nginx exists
access_log /var/log/nginx/wordpress_access.log;
error_log /var/log/nginx/wordpress_error.log;
index index.php;
location / {
try_files $uri /index.php$request_uri$is_args$query_string;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht { # avoid downloading htaccess, htpasswd, etc files
deny all;
}
}
you can try this vhost file
server {
listen 80;
server_name www.example.com example.com;
root /var/www/www.example.com/web;
if ($http_host != "www.example.com") {
rewrite ^ http://www.example.com$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
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;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}