Nginx to serve php files from another server

I am trying to configure nginx to serve PHP from another server.

Files may be in the directory under / sample on another server

Fast CGI runs on port 9000 on another server

Here is what I tried, which is not working right now.

location ~ [^/]\.php(/|$) { proxy_pass http://192.168.x.xx; proxy_redirect http://192.168.x.xx /sample; fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # Mitigate https://httpoxy.org/ vulnerabilities fastcgi_param HTTP_PROXY ""; fastcgi_read_timeout 150; fastcgi_buffers 4 256k; fastcgi_buffer_size 128k; fastcgi_busy_buffers_size 256k; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } 

I also need to configure nginx to serve static files from the same server

+5
source share
4 answers

The following configuration does exactly what you need:

 server { listen 80; index index.php index.html; server_name localhost; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; root {STATIC-FILES-LOCATION}; location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass {PHP-FPM-SERVER}:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; } } 

All you have to do is replace {STATIC-FILES-LOCATION} with the location of your static files on the Nginx server and {PHP-FPM-SERVER} using the PHP-FPM IP server.

Thus, you will serve all files without the PHP extension statically from the Nginx server, and all PHP files will be interpreted with the PHP-FPM server.

Here is a working example of a docked version of what you are trying to achieve - https://github.com/mikechernev/dockerised-php/ . It serves for static files from Nginx and interprets PHP files through the PHP-FPM container. In the accompanying blog post ( http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/ ) I will talk in detail about the whole connection between Nginx and PHP-FPM.

EDIT: It is important to remember that the paths on the Nginx and PHP-FPM servers must match. Therefore, you will need to put your php files in the same directory on the PHP-FPM server as your static files on Nginx one ( {STATIC-FILES-LOCATION} ).

An example would be /var/www/ in Nginx, where your static files are stored and /var/www in PHP-FPM to store your php files.

Hope this helps :)

+3
source

You should not use proxy_* directives . using Nginx as a proxy server will only be performed if the remote server displays the page (and you request it with the HTTP protocol).

Here you want a proxy server fastcgi server , not an HTTP server.

So the key is:

 fastcgi_pass 127.0.0.1:9000; 

Where do you say now that you want to reach the fastcgi server on the IP port 127.0.0.1 900, which seems completely wrong.

Use instead:

 fastcgi_pass 192.168.x.xx:9000; 

And remove the proxy_* stuff.

Edit : also, as pointed out by @Bart's comments, you should not use an if check so that the local file in the document root matches the php script name. Php files are not located on this server . Therefore delete this file. If you want to apply some security checks, you will subsequently change your general location [^/]\.php(/|$) to something more specific, for example, location=/index\.php or some other options.

+2
source

You do not need to use proxy_ directives because they work with the HTTP protocol, but in this case the FastCGI protocol is used. Also, as the comments said, there is no need for an if because the Nginx server cannot determine if the file exists on the remote server.

You can try this configuration:

 location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; # Mitigate https://httpoxy.org/ vulnerabilities fastcgi_param HTTP_PROXY ""; fastcgi_read_timeout 150; fastcgi_buffers 4 256k; fastcgi_buffer_size 128k; fastcgi_busy_buffers_size 256k; fastcgi_pass 192.168.x.xx:9000; #not 127.0.0.1, because we must send request to remote PHP-FPM server fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /path/to/site/root$fastcgi_script_name; } 

You will need to replace /path/to/site/root with the real path on the PHP-FPM server. For example, if the request http://example.com/some/file.php be processed by /var/www/some/file.php , then set it like this:

 fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; 

In addition, so that the PHP-FPM server can receive requests from outside, edit the configuration of the FPM pool (on Debian it is usually located in /etc/php5/fpm/pool.d/www.conf , on Centos - /etc/php-fpm.d/www.conf ):

Replace

 listen = 127.0.0.1:9000 

with:

 listen = 9000 

or

 listen = 192.168.x.xx:9000 # FPM server IP 

You will probably also need to edit the allowed_clients directive:

 listen.allowed_clients = 127.0.0.1,192.168.y.yy # Nginx server IP 

I also need to configure nginx to serve static files from the same server

If I understand correctly and you want to serve static files from the server, Nginx works, you can simply add another location to your Nginx configuration.

+2
source

No need to specify / sample path

 location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # Mitigate https://httpoxy.org/ vulnerabilities fastcgi_param HTTP_PROXY ""; fastcgi_pass IP:9000; fastcgi_index index.php; include fastcgi_params; } 

For static files from nginx server you need to use try_files .

 location / { try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; // other CGI parameters } 

Make sure you know the common pitfalls .

If you want to access static files from another server, you need to run a web server there, and only the proxy will go from Nginx

0
source

Source: https://habr.com/ru/post/1269126/


All Articles