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.
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 want to setup multiple projects on nginx locally.
These are my two server blocks:
server {
listen 80;
listen [::]:80;
root /var/www/site1.dev/html;
index index.php index.html index.htm;
server_name site1.dev;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
listen [::]:80;
root /var/www/site2.dev/html;
index index.php index.html index.htm;
server_name site2.dev;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Pinging site1.dev site2.dev will give a response.
In the browser going to localhost will redirect to site1.dev
Browsing to site1.dev will redirect to https://site1.dev and will return "This site can’t be reached"
Did I miss anything? According to the tuts these blocks are correct.
You better don't use dev domain.
Chrome & Firefox now force .dev domains to HTTPS via preloaded HSTS
I recently moved a website which used Apache to Nginx.
How can I get an url which looks like this:
http://example.com/login
to point to http://example.com/login.php while keeping the .php out of the url?
My current configuration:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html/;
index index.php index.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
# deny accessing php files for the /assets directory
location ~ ^/assets/.*\.php$ {
deny all;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
I usually use something like this and it's SEO proof. Tested by a lot of my client sites, works like a charm.
In your /etc/nginx/conf.d/domain.tld.conf file include this :
server {
index index.html index.php;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
Source i used a while a go
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?
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.