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
Related
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 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'm using something like the following as a "default" vhost on Nginx. I want every sub domain to have an own directory basically.
Can anyone help with a fallback (I'm new to this). If a directory/sub domain doesn't exists I want some kind of custom 404.html page.
Thanks!
server {
server_name ~^(.+).mysite.com$;
set $root_path $1;
root /var/www/$root_path/public;
index index.html index.php;
location / {
try_files $uri /$uri /index.php?$args;
}
location ~ \.php$ {
#Include Nginx’s fastcgi configuration
include /etc/nginx/fastcgi.conf;
#Look for the FastCGI Process Manager at this location
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
client_max_body_size 100m;
}
}
Try this:
server {
...
error_page 404 /your_custom_404.html;
location = /your_custom_404.html {
root /path/to/your_custom_404.html;
internal;
}
location / {
try_files $uri $uri/ /index.php?$args =404;
}
...
}
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;
}
My problem is following: I use Wordpress on Nginx with "pretty links". I also run 2 other services on ports 88 and 1234 and I want to make a subdomains bugs.mydomain and mail.mydomain. I did the proxypass on location / but it's working only for the main directory, anything that is after the domain/ is falling into Wordpress "pretty links" mechanism. Do you have any idea how to solve this? My config files below:
The server config:
server {
listen <IP>:80;
root /usr/share/nginx/www/domain;
index index.html index.htm index.php;
server_name domain www.domain;
location / {
try_files $uri $uri/ /index.html;
if ( $host ~ "bugs.domain" ) {
proxy_pass http://domain:88;
}
if ( $host ~ "mail.domain" ) {
proxy_pass http://domain:1234;
}
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
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;
}
location ~ /\.ht {
deny all;
}
include /home/domain/public_html/nginx.conf;
}
the config for specified domain (with Wordpress):
#First there is many rewrites for the W3TC plugin, like minification, caches etc
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
#
# unless the request is for a valid file, send to bootstrap
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
Now, when I enter domain:88 or domain:1234 it works. When I enter bugs.domain the website loads, but no CSS or images works as the url is bugs.domain/somapath and this falls into the Wordpress bootstrap. I run out of the ideas.
why create only 1 server with if's in it, separate the servers
server {
listen 80;
server_name bugs.example.com;
proxy_pass http://example.com:88;
}
server {
listen 80;
server_name mail.example.com;
proxy_pass http://example.com:1234;
}
server {
listen 80;
# the rest of your main server
#
}
So the problem was completely different then I thought. it was failing on this line:
try_files $uri $uri/ /index.html;
The problem was, that file index.html didn't exist, I only had index.php. Changing it solved the problem.