NGINX - Mapping html files to php - php

I cannot make nginx read php code embedded in .html files for a specific directory (html/test). I run CentOS 6 and have FastCGI installed. I have tried several configurations, but nothing does the job. My nginx.conf looks like this:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name shop-munk.com *.shop-munk.com;
location / {
root /opt/html;
index index.html index.htm index.php;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /opt/html;
}
location ~ \.php$ {
root opt/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.html$ {
root opt/html/test;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.html;
fastcgi_param SCRIPT_FILENAME /$fastcgi_script_name;
include fastcgi_params;
}
}
}

I was trying to do the same thing and server returned "403 Forbidden" for scripts without .php extension.
Following PHP-FPM configuration fixed the problem:
security.limit_extensions = .php .html

Related

Nginx unable to access parts of the website

Si I'm trying to run Invision Forums on nginx but it can't seem to access the uploads folder for some reason. It's most likely my configuration that's wrong but I'm not really sure since this is the first time I'm working with nginx.
Here's my configuration file:
worker_processes 1;
user www-data;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/forums;
index index.php;
charset utf-8;
client_max_body_size 100M;
fastcgi_read_timeout 1800;
location / {
try_files $uri /index.php;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

Nginx virtual block on Mac pointing to localhost's index.php

I am configuring virtual host to serve .localhost domain on my mac. But when I open "project.localhost" in my browser it shows the same page as, browsing the index of localhost. (ps: I've also configured dnsmasq and /etc/hosts). (trying to serve index.php)
Here is my nginx.conf:
worker_processes 1;
events {
worker_connections 1024; }
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 64;
client_max_body_size 50M;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root html;
location / {
# root html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
# root html;
}
location ~ \.php$ {
# root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
include servers/*; }
and here is sites-available:
server {
listen 80;
listen project.localhost:80;
server_name project.localhost;
location / {
root html/project.localhost/public_html;
index index.php index.html index.htm;
try_files $uri $uri/ =404;
}
location ~ \.php$ {
root html/project.localhost/public_html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and here is dnsmasq.conf:
address=/.localhost/127.0.0.1
and here is /etc/hosts:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 project.localhost www.project.localhost
but when I browse project.localhost in my browser it shows same page as localhost/index.php. What am I doing wrong?
As #Richard mentioned in the comment section I was able to solve the issue by including sites-available/*.conf: include /usr/local/etc/nginx/sites-available/*.conf, just before include server/* into my nginx.conf. Thank you #Richard

nginx url rewrite for angularjs

I am building a website based on angularjs and nginx server.I am struggling at url rewrite for angularjs , I used my development environment as apache which is working well.I want to use extensionless php so that if i visit www.mywebsite.com/register it points to www.mywebsite.com/register.php . When I put slash near www.mywebsite.com/register/it is not working.For angular I want the url as www.mywebsite.com/register/step2 to www.mywebsite.com/register/#!/step2
server {
server_name mywebsite.com;
return 301 $scheme://www.mywebsite.com$request_uri;
}
server {
listen 80;
server_name localhost www.mywebsite.com;
#charset koi8-r;
#access_log /var/log/nginx/log/india.access.log main;
location / {
root /var/www/mywebsite;
index index.php index.html index.htm;
}
location ~ /register/ {
rewrite ^ register.php/#/$1 redirect;
break;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/mywebsite;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location /api/ {
try_files $uri $uri /api/index.php?$query_string;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
root html;
#include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/mywebsite$fastcgi_script_name;
include fastcgi_params;
fastcgi_intercept_errors on;
error_page 404 /404;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# if enable status in PHP-FPM config
location ~ ^/(status|ping)$ {
allow 127.0.0.1;
#deny all;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}

Nginx rewrite rule for codeigniter for URL parameters

I did the following configurations for nginx server for code igniter and worked correctly , unfortunately there is one case not working.
When I request URL with parameter Like
servername.com/ControllerName/methodName?param=value
I got Not found Page 404
Server Configurations :
server {
# access from localhost only
listen 127.0.0.1:80;
server_name serverName.com;
root API;
log_not_found off;
charset utf-8;
access_log logs/accessservername.log main;
index index.php;
# handle files in the root path /www
location / {
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root API;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9100
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php;
fastcgi_index index.php;
#fastcgi_param PHP_FCGI_MAX_REQUESTS 1000;
#fastcgi_param PHP_FCGI_CHILDREN 100;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REMOTE_ADDR $http_x_real_ip;
include fastcgi_params;
}
# add expire headers and speed up image access with a vary header
location ~* ^.+.(gif|ico|jpg|jpeg|png|flv|swf|pdf|mp3|mp4|xml|txt|js|css)$ {
expires 30d;
add_header Vary Accept-Encoding;
}
# only allow these request methods
if ($request_method !~ ^(GET|HEAD|POST)$ ){ return 405; }
}

phpmyadmin and nginx gives no input file

This is my primary conf file
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mysite.com www.mysite.com;
root /home/mysite/www;
sendfile off;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
index index.php index.html index.htm;
location / {
}
location /pma {
alias /usr/share/phpmyadmin;
location ~ \.php$ {
try_files $uri #php;
}
}
location #php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
The phpmyadmin files are in /usr/share/phpmyadmin/
If I go to www.mysite.com/pma/ it gives me
No input file specified
And in error log :
2016/06/12 15:48:47 [error] 15999#0: *1 FastCGI sent in stderr: "Unable to open primary script: /home/mysite/www/pma/index.php (No such file or directory)" while reading response header from upstream, client: my-ip, server: mysite.com, request: "GET /pma/ HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/www.sock:", host: "my server ip"
I've tried everything, changing alias with root also didn't help, it was downloading the index.php file to a file named 'download'
I've read that I should use alias instead of root. But it did not seem to solve my problem...
EDIT
i also have this inside default.d which is included
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $uri =404;
fastcgi_intercept_errors on;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass php-fpm;
}
Please change alias to root ,and make sure correct path to your phpmyadmin in /usr/share/pma.
in this config use socket php-fpm. and sorry for my bad english
# # configure phpmyadmin path
location /pma {
root /usr/share;
index index.php;
location ~ ^/pma/(.+\.php)$ {
try_files $uri $uri/ /index.php?$args;
root /usr/share;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
#fastcgi_param PATH_INFO $fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/pma/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share;
}
}

Categories