Im using backbone and nginx. I have a location as follows:
# will be handled with nginx
location ~* ^/ {
root /Users/myname/Projects/cms/source/cms;
access_log off;
expires max;
}
Using the following url works:
http://localhost/console.html
Using the following url fails with a 404:
http://localhost/console.html/#login
Ive tried the following:
location ~* \/(#)$ {
location ~* \(#)$ {
But doesn't work. Any suggestions?
Update
The location was actually correct. Thanks to mu for clearing up it that it wasn't my attributes but the trailing slash.
The location was actually correct. Thanks to mu for clearing up it that it wasn't my attributes but the trailing slash.
Related
I have a create-react-app that I built, created npm run dist and copied the resulting dist files to /var/www/html/build/
Here is my nginx config:
server{
listen 9095;
server_name <hostname>;
access_log /var/log/nginx/nginx.pass.access.log;
error_log /var/log/nginx/nginx.pass.error.log;
root /var/www/html/build/;
location / {
index index.html;
try_files $uri $uri/ /index.html;
}
}
With this, I can access http://hostname:9095.
What I actually need is to serve the same site with http://hostname:9095/pass (or /PASS) (and not /)
I have tried various re-write config but I can't seem to figure this out.. Help appreciated
The format of a rewrite expression is rewrite <regex> <replacement> [flag];. The simplest way to redirect /pass or /PASS to your /var/www/html/build/index.html page is as follows.
location ~* ^/pass$ {
rewrite ^/(.*)$ /index.html break;
root /var/www/html/build;
index index.html;
}
The location ~* ^/pass directive is a case-insensitive regular expression match against the /pass URI. The ^ symbol ensures the regex matches only the /pass uri, and not something like /some/dir/pass, while the $ symbol ensures a url like /password is not matched as well.
Note, however, that this regex will match /pAsS, /PAss, and /pasS. In other words, it really is case-insensitive. If you wish to match only /pass or /PASS, you should modify the regex accordingly.
Better yet, you can completely circumvent regex URI matching by using an exact URI match.
location = /pass {
...
}
Using an exact match like this speeds up request processing because regular expressions do not need to be parsed.
To match /PASS, you could then simply define another exact location match block.
location = /PASS {
...
}
This then frees up your main location block for your primary server content.
location / {
root /var/somewhere/else;
index index.html;
}
Edit: After writing this, I came across the alias directive, which also seems promising. I haven't used it myself (I've always used rewrites for this kind of thing), but it's something else to explore as well.
I am trying to load two different react app one on root level and one on sub location i.e /in. What I am doing is I am checking if $uri consist /in /t if that condition is true it will change root path after setting root path I am serving index file. Its not working its throwing 404 not found when i am trying to access domain.com/in/something and domain.com is working fine.
I don't know is it right way to do this or not. I tried this
Here is my nginx config file:
server {
server_name domain.com;
rewrite ^/(.*)/$ /$1 permanent;
set $rootpath "/var/www/html/website/public";
location / {
if ($uri ~ \/(in|t)\/){
add_header X-uri "$uri";
set $rootpath "/var/www/html/app/build";
}
root $rootpath;
try_files $uri /index.html;
}
}
I have installed nginx on OS X with php7.1, mysql and so on... and the basic testing example is working.
When I try to configure nginx to serve Laravel backend on user.domain.dev/api/... and Angular frontend on user.domain.dev/... I am getting 404 or 403. Nginx error log is mostly
/Users/name/Projects/domain.dev/api.domain.dev/" is forbidden
or
/Users/name/Projects/domain.dev/frontend.domain.dev/build/index.php" failed (2: No such file or directory)
I can't seem to understand nginx's location directive and so they're pointing to a wrong directories. Thanks for any advices or guidance.
My config is:
nginx.conf
user name staff;
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
ssl_certificate ssl/nginx.crt;
ssl_certificate_key ssl/nginx.key;
access_log /usr/local/var/log/nginx/access.log;
error_log /usr/local/var/log/nginx/error.log;
sendfile on;
keepalive_timeout 5;
gzip on;
include servers/*.conf;
}
servers/domain.dev.conf
server {
listen 80;
listen 443 ssl http2 default_server;
server_name domain.dev *.domain.dev www.domain.dev;
charset utf-8;
# 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;
}
##
## Backend HTTP server
##
location /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
index index.php;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
}
##
## Frontend HTTP server
##
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location / {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
index index.html;
}
location ~ /\. {
deny all;
}
}
The main problem is an inconsistent use of the root directive. You have two document roots, one for each application, but only specified in two of your locations. There is no root specified at the server { ... } block level, therefore if (!-d $request_filename) is meaningless, location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ will always result in a 404 response, etc.
Read this document to understand how nginx processes a request.
But you should probably set the frontend root in the server block and override it with the backend root in the location /api block.
server {
root /Users/name/Projects/domain.dev/frontend.domain.dev/build;
location / { ... }
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ { ... }
location ^~ /api {
root /Users/name/Projects/domain.dev/api.domain.dev;
...
}
}
This would place the backend document root at: /Users/name/Projects/domain.dev/api.domain.dev/api -- note that the URI is always appended to the root.
Note also, the ^~ modifier is used to make the prefix location take precedence over regular expression locations at the same level. See this document for details.
I do not like the naked if blocks in your design. The if (!-d $request_filename) should probably be replaced with a try_files directive, and if ($host ~* ^www\.(.*)) should probably be replaced by using a separate server block. See this document for more.
The try_files statement in your location /api block contains an incorrect default URI, it should probably be /api/index.php?$query_string.
I'm trying to add on cakephp on to an existing server, but the location / block is being used. I'm following the pretty url on nginx section on the cakephp cookbook. On my test environment, I have the server block looking like
server {
listen 80;
server_name localhost;
access_log /var/www/html/log/access.log;
error_log /var/www/html/log/error.log;
location / {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
From this, I'm able to run my testsController through the url localhost/tests
However, the server that I'm trying to add cakephp to, there is already another application installed at the domain root.
location / {
proxy_pass https://localhost/somepage;
}
I tried setting up a location block like
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
I understand that this wouldn't work because it's looking for cakephp in the url, which it wouldn't be there. Since root is set to be /var/www/html/cakephp/app/webroot, when I access the url localhost/cakephp, is it looking for /var/www/html/cakephp/app/webroot/cakephp?
I'm getting confused about how to set this up. I read about url rewriting and cakephp running in some subdirectory, but I'm not sure if that's what I am looking for. Right now, the application runs with http://localhost/someController. I would like to have the the application run with the url http://localhost/cakephp/someController. How should I setup my nginx config?
Fix static files first
With the config in the question, what you'll find is that nothing really works - not even requests for static files.
Consider:
server {
...
root /wherever/;
error_log /tmp/cakephp.err.log debug; # <- add this
location /cakephp {
root /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
This will produce:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 404 Not Found
The debug log will help to clarify why this occurs:
-> cat /tmp/cakephp.err.log
...
2015/08/23 10:53:43 [debug] 9754#0: *87 http script var: "/cakephp/favicon.ico"
2015/08/23 10:53:43 [debug] 9754#0: *87 trying to use file: "/cakephp/favicon.ico" "/var/www/html/cakephp/app/webroot/cakephp/favicon.ico"
Nginx is using the whole url as the path to a file, not just the bit after the location prefix. This is where understanding the difference between the root directive and the alias directive is important, and is also a common question (random result, there are many).
So, fixing that first:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/; # <- alias, not root
index index.php;
try_files $uri $uri/ /index.php?args;
}
}
Will produce:
$ curl -I http://cakephp.dev/cakephp/favicon.ico
HTTP/1.1 200 OK
Then fix php requests
The problem with php requests is more or less the same thing; though the request finds the right php file it's configured such that CakePHP will assume it's installed in the root. There are various solutions to this - here's one:
server {
...
error_log /tmp/cakephp.err.log debug;
location /cakephp {
alias /var/www/html/cakephp/app/webroot/;
index index.php;
try_files $uri $uri/ /cakephp/index.php; # <- redirect to the actual equivalent request
}
location /cakephp/index.php {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
# Explicit script filename
fastcgi_param SCRIPT_FILENAME /var/www/html/cakephp/app/webroot/index.php;
}
}
In this way static files and dynamic requests both work - and the environment variables that CakePHP receives are such that it understands the root of the application to be /cakephp/.
I am having a really hard time getting nginx working with CakePHP in a subdirectory.
I'm using nginx because I am only familiar with Apache and I want to learn something new. My goal is to host CakePHP in a subdirectory.
My /etc/nginx/sites-available/default file looks like this:
server {
listen 0.0.0.0:80;
root /var/www/;
index index.html index.php;
include /etc/nginx/include/php;
error_page 404 = /404.html;
location / {
root index.html;
index index.php index.html index.htm;
autoindex on;
try_files $uri $uri/ /index.php;
}
location /randodb {
if (!-e $request_filename) {
rewrite ^/randodb(.+)$ /randodb/app/webroot/$1 last;
break;
}
}
location /randodb/app/webroot {
if (!-e $request_filename) {
rewrite ^/randodb/app/webroot/(.+)$ /randodb/app/webroot/index.php?url=$1 last;
break;
}
}
}
server {
listen 0.0.0.0:443;
root /var/www/;
index index.html index.php;
fastcgi_param HTTPS on;
include /etc/nginx/include/ssl;
include /etc/nginx/include/php;
error_page 404 = /404.html;
}
No matter what I do, any requests in the /randodb folder get a 301 redirect.
It always redirects me to http://nginx-php-fastcgi/randodb/
If you want to see this behavior in action: http://www.matgilbert.com/randodb
First this is the only block you need
location ~ /randodb(.*) {
root /var/www/randodb/app/webroot;
try_files $1 $1/ /index.php?url=$1;
}
above in the / block, you specified root index.html; what is this supposed to do, cause I believe this is incorrect, root is supposed to be a directory.
About this block
fastcgi_param HTTPS on;
include /etc/nginx/include/ssl;
include /etc/nginx/include/php;
is php working ? never seen it before, are u using something different than fast-cgi or fpm?
then the ssl block, here's my ssl block on my server
server {
listen 443 ssl;
ssl_certificate /etc/ssl/name.crt;
ssl_certificate_key /etc/ssl/name.key;
server_name example.com
location / {
# the rest of my config
}
}