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.
Related
so.. here appear the css don't working and the link with .php cause without .php don't work just got 404 not found nginx.
and my nginx config is
server {
listen 80;
listen [::]:80;
root /var/www/painel.site.com.br;
index index.php index.html index.htm;
server_name painel.site.com.br www.painel.site.com.br;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
my assets is on /var/www/painel.site.com.br/assets
I'm trying to setup a basic nginx server. Usually I'm not working with nginx and I ran in to some issues I'm unable to wrap my head around.
I have a debain server with all the necessary things installed (like php, mariadb, ufw,...) and I want to run my website somewhat like this:
xxx.xxx.xxx.xxx -> /var/www/
http(s)://(www).lechner.io -> /var/www/domains/lechnerio/
I want both https and http and the domain with www and without pointing to the folder /var/www/domains/lechnerio and the IP Address pointing to /var/www/
First things first, only getting :80 working.
I have the following config setup:
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
however, when I now try to visit the IP the nginx welcome site is shown. When I access it via domain, it also shows the files from /var/www/ even though i reloaded everything. nginx -t is working. A link from /etc/nginx/sites-available/lechnerio to /etc/nginx/sites-enabled/
Any input very welcome!
try following
server {
listen 80;
listen [::]:80;
root /var/www/doamins/lechnerio;
index index.php index.html index.htm;
server_name lechner.io www.lechner.io;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/etc/php/7.3/fpm/php-fpm.conf;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/;
index index.html index.htm;
server_name _; # everything else
location / {
try_files $uri $uri/ =404;
}
}
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
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.
I still struggling with making mystaticusername variable user name. I mean when I have particular username /statisusername/ backend working properly on nginx. But how can I make my configuration for more users? Means use instead mystaticusername some redirection where each username will know login to backend.
How can I change my nginx configuration?
server {
server_name example.com www.example.com;
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/www;
charset utf-8;
index index.html index.php /index.php;
location = / {
rewrite ^ /index.php;
}
location / {
rewrite ^([^\.]*)$ /$1.php;
rewrite ^/([A-Za-z0-9_]+)$ /admin/index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location /mystaticusername {
try_files $uri/ /admin/index.php?q=$uri&$args;
}
location = /mystaticusername/options {
try_files $uri $uri/ /admin/index.php?hotelname=$1&do=options;
}
}
Not much context given, so I'm just guessin... but looks like you could condense your location directives to 3 statements.
This will default to index.php, but in the case of example.com/mystaticusername if would try that url and since it does not exist (guessing) it would serve up /admin/index.php?q=$uri&$args. This would also allow urls like exmaple.com/about to resolve. Just make certain to not allow users to create a username of 'about'
location / {
try_files $uri $uri/ /admin/index.php?q=$uri&$args;
}
Not certain what your goal is, but this would get you example.com/mystaticusername/options to try serve up /admin/index.php?hotelname=$1&do=options
location ~* ^/[a-zA-Z0-9]*/options$ {
try_files $uri /admin/index.php?hotelname=$1&do=options;
}
Leave php as-is
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}