I'm fairly new to Nginx, and I'm attempting to import legacy sites from Apache. I want to get rid of the virtual directories but unfortunately I cannot.
The virtual directories point to the same root at the primary site. There is logic in the code that detects the virtual directory and loads data based on that information, which is why it's there.
Here is the config I'm attempting to get to work:
server {
listen 80;
large_client_header_buffers 4 32k;
server_name site.domain.com;
access_log /var/log/sites/site.access.log;
error_log /var/log/sites/site.error.log error;
location / {
root /var/www/php/site;
index index.php;
include /etc/nginx/conf.d/php.inc;
}
location /sitea {
root /var/www/php/site;
index index.php;
include /etc/nginx/conf.d/php.inc;
}
location ~ /\.ht {
deny all;
}
}
Contents of php.inc:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
I have tried every thing that google has to offer to get this to work, but no matter what I do I continue to get the following error:
2018/11/15 20:28:32 [debug] 5056#5056: *1 http script var: "/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "/sitea/index.php" "/var/www/php/site/sitea/index.php"
2018/11/15 20:28:32 [debug] 5056#5056: *1 trying to use file: "=404" "/var/www/php/site=404"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http finalize request: 404, "/sitea/index.php?requestType=home" a:1, c:1
2018/11/15 20:28:32 [debug] 5056#5056: *1 http special response: 404, "/sitea/index.php?requestType=home"
2018/11/15 20:28:32 [debug] 5056#5056: *1 http set discard body
2018/11/15 20:28:32 [debug] 5056#5056: *1 xslt filter header
2018/11/15 20:28:32 [debug] 5056#5056: *1 HTTP/1.1 404 Not Found
Any help towards the right direction will be appreciated.
Note: it also does the same thing using the alias vs root
With alias I get the following:
2018/11/15 20:37:38 [error] 5189#5189: *1 FastCGI sent in stderr:
"Primary script unknown" while reading response header from upstream,
client: #.#.#.#, server: site.domain.com, request: "GET
/sitea/index.php?requestType=home HTTP/1.1", upstream:
"fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "site.domain.com"
Possible Solution
I'm not sure if this is the proper way of doing it, but it worked.
If I created a symbolic link for the virtual directory in the main folder the site loaded. I'd rather do this in Nginx, but if I have to go this route I will. Thoughts?
The root directive constructs the path to the file by the simple concatenation of its value with the current URI. So your second location block is looking for the file at /var/www/php/site/sitea/index.php.
The alias directive within a prefix location will replace the prefix text with the alias value. See this document for more.
location /sitea {
alias /var/www/php/site;
...
}
So the above location block will look for the URI /sitea/index.php at /var/www/php/site/index.php.
Both the root and alias directives set a variable called $request_filename to the path to the file.
In your PHP block, you use $document_root$fastcgi_script_name to inform PHP-FPM of the path to the file. This works with root but not with alias.
fastcgi_param SCRIPT_FILENAME $request_filename;
The above works with both root and alias for PHP blocks which do not process path info (such as yours).
Related
I am setting up fastcgi on nginx. I sort of have it working. Inside my location block if i have factcgi_param SCRIPT_FILENAME /var/www/cgi-bin/hello.cgi; it works. However I don't want to hard code the link.
Setting fastcgi with the below configuration
factcgi_param SCRIPT_FILENAME /var/www/cgi-bin/$fastcgi_script_name;
I get the following error
cannot get script name are document_root and script_name or (script_file) set and is the script executable?
while reading response header upstream from upstream. client 127.0.0.1,
server _, request "get /cgi-bin/hello.cgi"
On the webpage I get 403 Forbidden
hello.cgi is executable and works when I hard code it.
My fastcgi_params
I installed nginx at centos7(VPS) I know this question are often asked here. I've been searching solution but my nginx get worse. so I post here.
when first time I tried open a php file broswer asked file download insted of display. I searched solution at web and adjust code. after that.
I can display "Welcome to nginx!" but when I tried to open index.php , I got "An error occurred." That what I made a file that
Could you teach me what is wrong my code please?
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
And this is my log
2019/04/14 03:33:51 [error] 3445#3445: *10 connect() failed
(111: Connection refused) while connecting to upstream,
client: 140.xxx.xxx.x, server: localhost, request:
"GET /index.php HTTP/1.1", upstream:
"fastcgi://127.0.0.1:9000", host: "xxx.xxx.xxx.x"
I am trying to configure nginx to serve up error pages from an s3 bucket.
To that end my configuration looks like this:
location / {
error_page 404 = #fallback;
}
location #fallback {
rewrite ^ /my-s3-bucket/404.html;
proxy_pass https://s3.ap-northeast-2.amazonaws.com;
}
My expectation is that anything that hits the website and is not found is then sent to the #fallback location. I then want to rewrite the URL with the actual location of my 404 page and send on to the s3 bucket. I don't want to just 302 redirect to the 404 page.
The problem is that the proxy_pass directive is not executed. Instead, it just looks for my rewritten URL locally.
See my access logs below:
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/sdfd" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
2019/01/07 03:05:42 [error] 85#85: *3 open() "/etc/nginx/html/my-s3-bucket/404.html" failed (2: No such file or directory), client: 172.17.0.1, server: www.dev.mywebsite.com.au, request: "GET /sdfd HTTP/2.0", host: "www.dev.mywebsite.com.au"
I made a request to www.dev.mywebsite.com.au/sdfd which wasn't found. 'sdfs' was rewritten to 'my-s3-bucket/404.html' but instead of then proxy passing that to https://s3.ap-northeast-2.amazonaws.com it looks for it in the local /etc/nginx/html directory.
My nginx version is 1.15.2
Use rewrite...break if you want the rewritten URI to be processed within the same location block. See this document for more.
For example:
location #fallback {
rewrite ^ /error/404.html break;
proxy_pass https://example.com;
}
I'm trying to configure nginx for my project. Step by steps:
Create config file myproject
--/etc/nginx/sites-available
----myproject
Content of myproject file
server {
listen 80;
listen [::]:80;
root "/path/to/html/My Project/company/myproject";
index index.php index.html index.htm index.nginx-debian.html;
server_name myproject.localhost;
location / {
try_files $uri $uri/ /index.php?$request_uri;
location = /index.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME "/media/path/to/html/My Project/company/myproject/index.php";
}
}
}
Restart service nginx and try to load myproject.locahost in browser, I got "File not found."
View latest line in nginx error log, I got below error
9055#9055: *4 stat() "/media/path/to/html/My Project/company/myproject/" failed (13: Permission denied), client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", host: "myproject.localhost"
9055#9055: *4 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: myproject.localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "myproject.localhost"
If I move it to a directory path without space, It seems to work.
How can I define this path to make it work?
You should not use path with spaces in file or directory names. But in general Unix file or directory names can contain any character other than / (which is always a directory separator) and null bytes (which you can't use =D). Try to not use spaces, $, ;, |, <, >, etc. in file or directory names, or you should escape them using backslash symbol \ behind escaping symbol.
I need to have my symfony app installed on the same domain as other webapps so I wanted it to sit in /dev/symfony_app path
I tried to use NginX Friendly PHP Framework but solutions from there do not work.
I have such nginx config and it does not work at all too. there is some problem with paths, nor root neither alias directive work for me.
location /dev/symfony_app/ {
root /home/.../public_html/web;
}
location ~ ^/dev/symfony_app/(app|app_dev|config)\.php(/|$) {
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param HTTPS off;
}
nginx error log:
request http://domain.com/dev/symfony_app/
2013/06/23 11:25:31 [error] 22549#0: *668
"/home/.../public_html/web/dev/symfony_app/index.php"
is not found (2: No such file or directory), client: *,
server: domain.com, request: "GET /dev/symfony_app/ HTTP/1.1", host: "domain.com"
request https://domain.com/dev/symfony_app
2013/06/23 11:25:37 [error] 22549#0: *668
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
request https://domain.com/dev/symfony_app/app_dev.php
2013/06/23 11:27:06 [error] 22549#0: *797
FastCGI sent in stderr: "Primary script unknown" while
reading response header from upstream, client: *, server: domain.com,
request: "GET /dev/symfony_app/app_dev.php HTTP/1.1", upstream:
"fastcgi://unix:/var/run/php-fpm.sock:", host: "domain.com"
Well, what are the dots doing there in your path? You can’t have a directory with three dots as name (at least this would be new to me). The error message from nginx is very specific in that regard. That path doesn’t exist.
server {
listen 80;
server_name _;
root /home/public_html/web;
location / {
location ~* ^/dev/symfony_app/(app|app_dev|config)\.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php-fpm.sock;
}
}
}
That should do the trick.
The absolute local path to the index file of your Symfony installation has to be /home/public_html/web/dev/symfony_app/index.php. A request to http://example.com/dev/symfony_app will map the above location.
I hope this helps, otherwise please leave a comment and describe what else is going wrong.