How can I force nginx to retrieve me id using this link http://www.website.com/event-details/46
Instead of http://www.website.com/event-details?id=46
Here is my nginx configuration file
server {
listen 80 default_server;
listen [::]:80 default_server;
include snippets/phpmyadmin.conf;
root /var/www/html/website;
index index.html index.php index.htm index.nginx-debian.html;
server_name website.com www.website.com
location / {
try_files $uri $uri/ #extensionless-php;
}
location #extensionless-php {
rewrite ^(.*)$ $1.php last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
Thank you
I found the solution
location /event-details {
try_files $uri $uri/ #rewrite;
}
location #rewrite {
rewrite ^/event-details/(.*)$ /event-details.php?id=$1;
}
It may help someone one day
Related
Good day,
Please tell me how to remove the parameters of the GET request from the main page of the site and so that the rest of the parameters are processed. That is, if there are any parameters on the main page when requested, make a redirect to the site without parameters.
server {
listen 80;
listen [::]:80;
root /var/www/public_html/public;
index index.php index.html index.htm;
server_name mysite.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
Not tested but somethings on below lines should help you out
server {
listen 80;
listen [::]:80;
root /var/www/public_html/public;
index index.php index.html index.htm;
server_name mysite.com;
location / {
location = / {
if ($is_args)
rewrite / permanent;
try_files $uri $uri/ /index.php?$query_string;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
}
}
Trying to configure my site to change root directory when there is a cookie var called "developer". This is on a Debian server.
This is my current site config file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /sites/live/;
if ($http_cookie ~ 'developer') {
root /sites/dev/;
}
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
But that gives me this error:
nginx: [emerg] "root" directive is not allowed here
What should I be doing? Thanks!
add root inside the location and the condition what you want
following is he my configuration working fine
location / {
root /var/www/myside;
index index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
if that not work use rewrite rule
The location / have a #rewriteIt but something is worg...
Fresh installation of NGINX into UBUNTU 16 LTS, all apt standard.
NGINX is ignoring my rewrite, why? how to fix for example.com/foo redirect to test.php?
Testing status and dynamic pages, all fine except the rewrite:
OK http://example.com
BUG (is not for 404) http://example.com/foo
OK http://example.com/?foo
OK http://example.com/index.php
OK http://example.com/test.php
With scripts
example.com
server {
server_name example.com www.example.com;
root /var/www/example.com/;
index index.php index.html index.htm;
location / { # ignoring here?
try_files $uri $uri/ #rewriteIt =404;
}
location #rewriteIt { # something wrong here?
rewrite ^/?([a-zA-Z0-9\-]+)/?$ test.php?obj=$1 last;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
Same when change to try_files $uri $uri/ #rewriteIt;.
default
server {
listen 80; ## listen for ipv4; this line is default and implied
listen [::]:80 default_server ipv6only=on; ## listen for ipv6
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/php/php7.0-fpm.sock;
}
}
The named location needs to be the last parameter of the try_files statement, replacing the =404 term. See this document for details.
There is also a second error. All URIs in nginx begin with a leading /, so the rewrite statement should specify /test.php?obj=$1 as the target.
I have a server that I want to use as a personal server, with all of my projects under /var/www.
I currently have two folders, /var/www/html and /var/www/site.
I want to be able to access these folders by the following URLs (123.123.123.123 is my server IP):
123.123.123.123/html and 123.123.123.123/site
Here is my default virtual host file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And here is the one I created for /var/www/site, called site:
server {
listen 80;
listen [::]:80;
# Because this site uses Laravel, it needs to point to /public
root /var/www/site/public;
index index.php index.html index.htm index.nginx-debian.html;
server_name 123.123.123.123;
location /site {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
But when I go to 123.123.123.123/site, it says 404 Not Found, so clearly I'm doing something wrong (and yes, I restarted nginx).
Please help!
You only need one server block, as both /html and /site live in the same server.
Use nginx -t to check that nginx really does restart without giving any errors.
As /site uses a complicated directory scheme, you will need to use a nested location block to get the paths correct.
Your first project seems to have a simple arrangement of static and PHP files. You can use a root /var/www; statement to map URIs beginning with /html to the html folder. See this document for more.
Something like this may work for you:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
location ^~ /site {
alias /var/www/site/public;
if (!-e $request_filename) { rewrite ^ /site/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
}
The default server does not need a server_name.
this is my site .conf file:
server {
listen [::]:80;
listen 80;
root /usr/share/nginx/html/suitecrm/;
index index.php index.html index.htm;
access_log /var/log/nginx/suitecrmaccess.log;
error_log /var/log/nginx/suitecrmerror.log error;
# Block access to stuff in the root
location ~* \.(pl|cgi|py|sh|lua|log|md5)$ {
return 444;
}
# Block access to data folders
location ~ /(soap|cache|upload|xtemplate|data|examples|include|log4php|metadata|modules|diagnostic|blowfish|emailmandelivery)/.*\.(php|pl|py|jsp|asp|sh|cgi|tpl|log|md5)$ {
return 444;
}
location / {
try_files $uri $uri/ =404;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
The paths are correct, I don't get what could be wrong here, any ideas please?
Have you looked at the suitecrm.log file to see what it says?