I'm not used to Nginx yet...migrating from Apache2. I do not understand the location and root directives...I've read that root is best to use over alias...so, I made all roots absolute paths.
Server Block here:
index index.php index.html;
location = / {
root /var/www/app;
}
location /chat/ {
root /var/www/project1/chat;
}
location /kanban/ {
root /var/www/kanban;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
}
When I have root /var/www/app; outside of a location block will the main index.php work. However, when adding location blocks with root paths, all I get are 404 pages. I hope that it is not required to add PHP location blocks for each defined location, surely that is not needed right?
All I want is, for www.site.com to go to /var/www/app/index.php and www.site.com/chat/ to go to /var/www/project1/chat/index.php, etc. for each of my projects.
To redirect mywebsite.com/chat to a different directory than the root of your website you would have something like the code below.
root var/www/app;
index index.html index.php;
location /chat/ {
alias /var/www/project1/chat/;
index index.php;
}
Related
I have implemented a php application in codeigniter and now want to deploy it to the nginx server. Before deploying I checked my nignx configuration on my localhost using MAMP server. It is working correctly. But, this configuration is not working on the live server. As a beginner in nginx, I am not understanding where is the mistake here. In live server, I can not write in the main nginx.conf file. I have a separate configuration file like "abc" for my application "abc". And all my application files are under "abc/xyz" directory. Here is my sample confuguration,
location /abc {
root /srv/www/htdocs/apps/;
index index.html index.htm index.php;
location /xyz {
try_files $uri $uri/ /abc/xyz/index.php;
}
location ~ \.php(\/(\w+))*$ {
try_files $uri =404;
rewrite (.+)\.php(\/(\w+))*$ $1.php break;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Here, I can see my welcome page https://myapplication/abc/xyz. But if I want to navigate other pages like https://myapplication/abc/xyz/other_pages, it is showing "404 Page not found". I have checked the other solutions but none of them is not working in this case. Thanks in advance for the help!
The location /xyz block is nested within the location /abc block. The nested block is required to precess URIs with a prefix of /abc/xyz.
If there are other regular expression location blocks surrounding your location /abc block, you should use the^~` modifier.
For example:
location ^~ /abc {
...
location /abc/xyz {
...
}
...
}
See this document for more.
Sorry for the late answer. It was actually very silly mistake. My controller page name was in small character. This is why it was not working. My configuration is okay. The first letter of the controller page should be in capital character. For example, my controller name is Home. So my php file name must be Home.php not home.php.
I'm trying to get wordpress running in a subdirectory of an existing nginx (v1.14) server. The rest of the server works fine. I've copied the relevant lines of my nginx.conf below. If I remove the lines that refer directly to the wordpress (wp) directory, then I get an error complaining that wp has called an undefined function "mysql_connect". When I leave the lines that directly refer to the wp directory, it complains about permission denied.
I've looked through a bunch of other posts about this but I can't seem to find any solutions that solve the issue for me. I've tried:
adding define( 'WP_USE_EXT_MYSQL', true ); to wp-config.php and toggling the boolean
adding / removing $uri/ from nginx.conf
chmodding the wordpress dir and contents (755 for the dir and 644 for the contents)
adding / removing index.php to the wordpress location definition in nginx.conf
I have no idea how to troubleshoot this any further.
...
http {
...
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.9000;
}
...
server_name domain.tld
root /some/dir
location / {
index index.html index.htm index.php;
}
...
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
...
I always seem to have problems with nginx configurations. My SPA is located at /mnt/q/app (pushstate is enabled) and the frontend root is located at client/public. Everything should be mapped to index.html, where the app picks up the route and decides what to do.
Full path to the index is /mnt/q/app/client/public/index.html.
I think I ran out of options by now. No matter what I do, I just get a 404 back from nginx, I think the configuration is simple enought and have no clue what's wrong.
server {
listen 80;
server_name app.dev;
root /mnt/q/app;
location / {
root /client/public;
try_files $uri #rewrites =404;
}
location #rewrites {
rewrite ^(.+)$ /index.html last;
}
}
Any help is appreciated.
If nginx views the file system from the root, then the root should be set to /mnt/q/app/client/public, and not either of the two values you are using.
The last element of the try_files directive can be a default action (e.g. /index.html), a named location or a response code. You have a named location in the penultimate element - which will be ignored.
Your named location should work, but is unnecessary, as try_files is capable of implementing it more simply. See this document for more.
For example:
root /mnt/q/app;
location / {
root /mnt/q/app/client/public;
try_files $uri $uri/ /index.html;
}
location /api {
}
location /auth {
}
The $uri/ element will add a trailing / to directories, so that the index directive can work - you do not have to add it if you do not need it.
Currently I'm running webprojects with the following directory structure (simplified):
project_folder/
public/
root/
index.php
What I want to do is set the root in the server block to:
root /project_folder/root/;
But when a request location does not exists I want it to forward the request to project_folder/index.php. I tried the following:
try_files $uri ../index.php?$query_string;
But this doesn't seem to work.
The same goes for a request that starts with $document_root/public/* which need to be forwarded to: $document_root/../public/*
I did manage this by adding a index.php in the /project_folder/root/ folder which includes the /project_folder/index.php. For the public folder I created a symlink. I could probably solve this by adding an alias for the location /public/ however I'm trying to keep my config as generic as possible.
I'm trying to prevent setting the project_folder as a root folder as it sometimes contains files I prefer not to be accessed from the web. Introducing the 'root' folder is new but I'm trying to do this as efficient as possible. Existing webprojects do not have the root folder yet.
Is what I'm trying to do possible and how would I be able to achieve this.
Thanks in advance.
Usually PHP files are processed by a location ~ \.php$ block (or similar). I assume that index.php is not the only PHP file in your application, and to process PHP files within the /root/ directory structure, that location will need to use root /project_folder/root.
You can specify a different root for URIs which begin /public and use a named location to execute the out-of-root index.php file.
Something like this:
root /project_folder/root;
location / {
try_files $uri $uri/ #index;
}
location /public {
root /project_folder;
}
location #index {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /project_folder/index.php;
fastcgi_pass ...;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
It is simple to add an alias:
location /public {
alias /absolute/path/to/public;
}
Basically I have a site, called example.com. Within this site, there is a WordPress-Blog, wich should be accessible under "example.com/blog/". The actual directory where the blog is installed is not "/blog/", but "/blog/de/", as this project has multiple stand-alone-blogs for different languages.
The problem is, that WordPress cannot access to wp-content and wp-include files, because WordPress tries to load css (and other) files via example.com/blog/wp-content/....
So i want to get my Blog running under "example.com/blog", but all WordPress-Files should be loaded from /blog/de/.
This is my nginx-vhost-config for /blog-Location:
location /blog {
index index.php;
try_files $uri $uri/ /de/index.php?$args;
rewrite ^(/wp-content/.*)$ /de/wp-content/$1 last;
rewrite ^/blog/(.*)+$ /blog/de/index.php?$1;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
I am totally new to nginx, so every possible solution is welcome!
Essentially you need to alias the location /blog to the path /blog/de. The complication is the way in which PHP files are handled, particularly when other applications are hosted within the same domain.
First internally rewrite /blog to /blog/de:
location /blog {
rewrite ^/blog(.*)$ /blog/de$1 last;
}
Then implement the /blog/de location with a nested location to handle PHP:
set $wordpress /blog/de/index.php;
location ^~ /blog/de {
# root inherited from server container???
index index.php;
try_files $uri $uri/ $wordpress;
location ~ \.php$ {
try_files $uri $wordpress;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass ...;
}
}
I make /blog/de a priority prefix location, to avoid a conflict with any other php location. But that would not be necessary if this is the only application within this server block.