nginx - php7.0-fpm php scripts no works with alias - nginx

i want to create routing like a:
hxxp://127.0.0.1/ <-- default with default location /var/www/ without listning directories
hxxp://127.0.0.1/allegro/
How to do it?
If i go to hxxp://127.0.0.1/allegro/scripts/test.php i see a blank page. If i go to hxxp://127.0.0.1/ php scripts excutes normally and i see phpinfo()
My nginx config:
server {
listen 8000 default_server;
listen [::]:8000 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php7.0-fpm.sock;
}
location /allegro/ {
alias /var/www/allegro/;
autoindex on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/run/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}

There are multiple issues with your current configuration. The most important is that the URI /allegro/scripts/test.php is not processed by the nested location block, because regular expression location blocks take precedence (unless a ^~ modifier is used).
location ^~ /allegro/ {
root /var/www;
# autoindex on;
try_files $uri $uri/ =404;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/run/php7.0-fpm.sock;
include fastcgi_params;
}
}
The ^~ modifier ensures that this prefix location takes precedence over regular expression locations at the same level. See this document for details.
Note that the root statement is preferred over an alias statement where applicable (see this document for details).
The fastcgi_split_path_info and fastcgi_index directives are unnecessary for location blocks which do not match URIs with path info.
You will need to decide whether include fastcgi_params or include snippets/fastcgi-php.conf is the more appropriate source for FastCGI parameters.

Related

Rewrite Rule Outputs PHP Source Code Instead of Running Script

I have a PHP script that takes two variables and creates a dynamic image. If I access the script directly in the browser it works perfectly. However, I'm trying to use a rewrite rule to make the url nicer looking. Instead of the script executing, the browser downloads the .php source code. What am I doing wrong?
server {
listen 80;
root /folder/subfolder;
index index.html;
server_name domain.com;
location / {
autoindex on;
try_files $uri $uri/ /index.html /index.php?$args;
rewrite ^/images/(\d+)/(\d+)\.png /_images.php?x=$1&y=$2 last;
}
location ~ ^(.+\.php)(.*)$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
}
}

nginx multiple root config issue

I have the following nginx config for multiple root (html/web is default, html/pma is additional route):
server {
listen 443 http2 ssl;
listen [::]:443 http2 ssl;
server_name website.com;
server_tokens off;
root /usr/share/nginx/html/web;
index index.php;
location / {
try_files $uri /index.php?$args;
}
location ^~ /pma {
root /usr/share/nginx/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
So, by default /html/web/index.php is opened, however website.com/pma opens /html/pma/, where pma is PHPMyAdmin.
The problem is:
PHPMyAdmin authentification form redirects to index.php. Therefore, when I write my credentials, it redirects me to /html/web/index.php! But should /html/pma/index.php. Even log out from PHPMyAdmin redirects to /html/web/index.php!
Could anyone suggest a better way of configuration?
I can see two errors in your configuration.
You try_files statement should end with /index.php or =404, not both! See this document for details.
The nested location ~ \.php$ block will never be consulted. The outer location ~ \.php$ block take precedence. You can solve this problem by using the ^~ modifier on the surrounding prefix location. See this document for details. For example:
location ^~ /pma {
...
location ~ \.php$ {
...
}
}
location ~ \.php$ {
...
}

nginx rewrite sub url

How can I solve this problem: I want to set up nginx conf file to meet below criteria:
http://www.example.com/site1/article/index.php?q=hello-world -> http://www.example.com/site1/article/hello-world
httb://www.example.com/site2/article/index.php?q=goodbye-world -> httb://www.example.com/site2/article/goodbye-world
httb://www.example.com/site3/article/index.php?q=open-new-world -> httb://www.example.com/site3/article/open-new-world
There are multiple sites after example.com, I want to make the url look clean by using nginx configuration.
But my below configuration doesn't work. Someone help me?
server {
listen 80;
listen [::]:80;
root /var/www/example.com/public_html;
index index.php index.html index.htm;
server_name www.example.com;
location ~ /article/ {
try_files $uri /site1/article/index.php?q=$1;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
}
You would like the client to present a URL like /xxx/article/yyy which is then internally rewritten to /xxx/article/index.php?q=yyy.
You need to capture the components of the source URI in order to use them later. You have a $1 in your question, but you are missing the expression to actually give it a value. With the minimum number of changes, this works:
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
location ~ \.php$ { ... }
}
However, you do not need to use a nested location for PHP, as long as the PHP regex location appears above the other regex location, it will process all php files. For example:
location ~ \.php$ { ... }
location ~ ^(.*/article/)(.*)$ {
try_files $uri $1index.php?q=$2;
}

Nginx multiple domains doesn't work

I have a perfect configuration which is nginx, php5-fpm, apc, varnish and mariadb. Everything works flawless except;
I am hosting a single web site, since my server resources are high and available, I want to host other web sites on the same server. When I try to add different websites into nginx the service simply does not restart.
here's my configuration file when everything works:
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
I don't want to use seperate files for different virtual hosts, I want to do everything in default file. But when I add another virtual host like below and save default file. nginx won't restart.
server {
listen 8080;
root /usr/share/nginx/www;
index index.php index.html index.htm;
server_name www.domain1.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
server {
listen 8080;
root /usr/share/nginx/domain2;
index index.php index.html index.htm;
server_name www.domain2.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
}
Please help me resolve this issue. I think something is conflicting but don't know what.
ok I found the solution just by investigating error log.
2014/08/19 21:55:07 [emerg] 5927#0: could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32
error log tells me to increase hash bucket size..
I edited nginx.conf and set the bucket size to 32 as advised in error log, it didn't work at first, but then I set it to 64 and it worked.
just search for "bucket" in nginx.conf, uncomment it, then set to 64 (or above in some cases) it will work, unless there is another issue.

Nginx Rewrite working except for root directory

I'd like to pass all requests that don't match a file to a script at /framework/root.php?path={whatever the request $uri was}.
This works nicely for all requests except for only /, for example http://localhost.
Here is my Nginx config:
PS: Only started using Nginx coming from Apache today.
server {
listen 80;
root /var/www/html;
server_name localhost;
location / {
try_files $uri $uri/ #root;
}
location #root {
rewrite ^(.*)$ /framework/root.php?path=$1;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
Oooo got it.
try_files $uri $uri/ #root; should be try_files $uri #root; without the $uri/ part because that matches the base / directory.

Resources