How to use Nginx to separate variables with slashes - php

I'd like to be able to use
www.example.com/profiles/1234567890
instead of
www.example.com/profiles?id=1234567890
but was unable to figure out how to do so.
I found an apache equivalent here: Turn text after slashes into variables with HTACCESS but I don't know how to get it to work in Nginx. I only need the one variable which is id.
Additionally, is it possible to do a rewrite such that
www.example.com/id/1234567890
points to
www.example.com/profiles/1234567890
but the url doesn't change?
Nginx config
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
listen 80;
server_name domain.com;
root /var/www/domain.com/public;
index index.php;
access_log /var/www/domain.com/access.log;
error_log /var/www/domain.com/error.log;
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
#rewrite ^(.+)$ /index.php?q=domain.com last;
rewrite ^(.*)$ $1.php last;
}
# catch all
error_page 404 /index.php;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
access_log off;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass 127.0.0.1:9000;
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/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Disable viewing .htaccess & .htpassword
location ~ /\.ht {
deny all;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ #extensionless-php?$args;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
# use fastcgi for all php files
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
#fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
# Some default config
fastcgi_connect_timeout 20;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
}
}

Inside the server block of your configuration file, try adding this:
rewrite ^/profiles?id=(.*)$ /profiles/$1 last;
rewrite ^/id/(.*)$ /profiles/$1 last;
Restart nginx. It should work.
More information can be found here: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

I think you put the rules swapped, try something like
rewrite ^/(?:profiles|id)/(.*) /profiles?id=$1 last;

For anyone looking for the answer, this worked:
rewrite ^/profiles/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;
rewrite ^/id/([a-zA-Z0-9_-]+)$ /profile.php?id=$1;

Related

Why in the global server variable, the query_param cell is not valid

I am trying to set up email verification, but because of wrong configuration of either, server or php I always get 403 Invalid signature error. If you know how to solve this problem I would be very grateful. All the solutions I googled don't work for me.
my route with params:
https://mysite.lo/email/verify/1001/82f42f0bbc6880958a68b56159cb7cbf96199ddf?expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
PHP DEBUG
$request->server->get('QUERY_STRING')
output:
/email/verify/1001/82f42f0bbc6880958a68b56159cb7cbf96199ddf&expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
enter image description here
But there should be another output, starting from ? to the end, for example:
expires=1642686658&signature=87fa7d09653adcbbeb4dd99bec9a97395d7417bdfeffc145a1c5d6e80feeb726
Or am I misunderstanding something?
Anyway, I don't understand why this is happening.
The nginx settings are below.
server {
listen 80;
listen 443 ssl;
listen [::]:80;
server_name mysite.lo *.mysite.lo;
ssl_certificate /etc/nginx/ssl/ssl.crt;
ssl_certificate_key /etc/nginx/ssl/ssl.key;
access_log /var/www/mysite/mpa/storage/logs/nginx_access.log;
error_log /var/www/mysite/mpa/storage/logs/nginx_error.log;
root /var/www/mysite/mpa/public;
index index.php;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
log_not_found off;
}
# removes trailing slashes (prevents SEO duplicate content issues)
if (!-d $request_filename)
{
rewrite ^/(.+)/$ /$1 permanent;
}
# enforce NO www
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
# unless the request is for a valid file (image, js, css, etc.), send to bootstrap
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
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;
}
location ~ /\.ht {
deny all;
}
}
This is the local environment.
I am using: laradock php 8
It is your rewrite ^/(.*)$ /index.php?/$1 last; rule adds an original URI as the first query argument. As rewrite directive documentation says:
If a replacement string includes the new request arguments, the previous request arguments are appended after them.
You can avoid adding an original URI as the first query argument specifically for the /email/verify/ route:
if (!-e $request_filename)
{
# do not append an original URI to this route
rewrite ^/email/verify/ /index.php last;
# but append it to everything else
rewrite ^/(.*)$ /index.php?/$1 last;
break;
}

Nginx configuaration for wordpress under different path

I have php website (symfony framework running in dev env) under example.com and wordpress blog under example.com/blog/ .
Now I'm trying make make wordpress working under this set up.
As of right now I have only static files served by nginx.
I can't make php files executed.
Symfony app root dir: /data/example.com
Wordpress root dir: /data/example.com-blog/web
My nginx config
server {
listen 80;
server_name example.com;
charset utf-8;
access_log /var/log/nginx/example.com_access.log main;
error_log /var/log/nginx/example.com_error.log;
root /data/example.com/web/;
index app_dev.php;
error_page 400 401 404 500 = /index.php;
location ~ /\. {
deny all;
}
location ^~ /blog {
log_not_found on;
access_log on;
root /data/example.com-blog/web;
rewrite ^/blog/([^.]+\.[^.]+)$ /$1 break;
try_files $uri $uri/ /blog/index.php$is_args$args;
location ~ \.php {
return 401; # for
}
}
location / {
try_files $uri /app_dev.php$is_args$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_intercept_errors off;
fastcgi_pass unix:/tmp/php-fpm.socket;
fastcgi_index index.php;
fastcgi_param HTTPS off;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|ico|js|css|htm|html|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|swf|flv|htc|xsl|eot|woff|ttf|svg)$ {
expires 1w;
tcp_nodelay off;
}
}
I tried to use RichardSmith's example and now I'm getting 403 error
location ^~ /blog {
alias /data/example.com-blog/web;
if (!-e $request_filename) { rewrite ^ /blog/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/tmp/php-fpm.socket;
}
}

WordPress wrong redirect while accessing wp-admin

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.

Running a secondary PHP app from a subdirectory in Nginx

I have a site built on the Kirby flat file CMS and its running in the root directory just fine, but I'm trying to get an instance of FlightPHP running in a subdirectory (called crm) of the above project. This FlightPHP instance will handle form submissions so I need to be able to map the urls correctly.
With Apache and .htaccess, it's easy:
# make forms/crm links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^crm/(.*) crm/index.php [L]
I just can't seem to get this working with Nginx, here's what I have so far:
server {
listen 80;
listen [::]:80;
root /var/www/mainsite;
index index.php index.html index.htm;
server_name mydomain.com;
autoindex on;
access_log off;
# Kirby Specific Directories
rewrite ^/site/(.*) /error permanent;
rewrite ^/kirby/(.*) /error permanent;
add_header X-UA-Compatible "IE=Edge,chrome=1";
location ~ .php {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 16k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
location ^~ /crm {
root /var/www/mainsite/crm;
if (!-e $request_filename)
{
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~* .(?:xml|ogg|mp3|mp4|ogv|svg|svgz|eot|otf|woff|ttf|css|js|jpg|jpeg|gif|png|ico)$ {
try_files $uri =404;
expires max;
access_log off;
log_not_found off;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
}
I have tried quite a few approaches from similar posts online, but none that work correctly.
Try replacing your location ^~ /crm block with this
location /crm {
try_files $uri $uri/ /crm/index.php?$args;
}
And if that one doesn't solve it try
location /crm {
try_files $uri $uri/ /crm/index.php?$uri;
}

nginx & wordpress - config for permalinks doesn't work; downloads php-files instead

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;
}
}

Categories