How do you point NGINX at standard PHP for multiple sites? - nginx

So I'm tasked with upgrading php that is used by NGINX in RHEL. I installed NGINX from the RHEL repo, but the instructions I followed to add PHP got it from the remi repo. I need to move over to PHP to meet the requirements of our Security team, but unsure how to configure it to do the same as I'm doing. I put this setup together with chicken wire and duct tape to support running 2 sites via NGINX. 1 is a rundeck site, the other is a wiki. With all the config, I'm confused as to how I would repeat the same without the remi install. Here's how I configure it:
Move nginx to port 8080 and separate off 2 sites for rundeck and wiki:
$vi /etc/nginx/nginx.conf (modify server block)
server {
listen 8080 default_server;
listen [::]:8080 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
$vi /etc/nginx/conf.d/rundeck.conf
server {
listen 8080;
listen [::]:8080;
server_name mymachine.mydomain;
access_log /var/log/nginx/mymachine.mydomain.access.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
location ^~ /wiki {
alias /var/www/wiki/html;
index index.php;
if (!-e $request_filename) { rewrite ^ /wiki/index.php last; }
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
location / {
proxy_pass http://localhost:4440;
proxy_set_header X-Forwarded-Host $host:$server_port;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Change the rundeck config to point to port 8080:
$vi /etc/rundeck/rundeck-config.properties
grails.serverURL=http://mymachine.mydomain:8080
Change the rundeck framework properties file to point to the same server name:
$vi /etc/rundeck/framework.properties
framework.server.name = mymachine.mydomain
framework.server.hostname = mymachine.mydomain
framework.server.port = 4440
framework.server.url = http://mymachine.mydomain:4440
Any help? Thanks!

This is a server administration task and has nothing to do with PHP programming...
PHP-FPM runs on a separate server, or in your case, in a separate process on the same server. Requests are forwarded to this FPM server via port 9000 (the fastcgi_pass instruction).
The nginx webserver listens on port 8080, but you can basically use any port you'd want. Nevertheless, if you configure the same "server" to listen on multiple ports, it's still the same site (= same document root directory) - which is btw missing in your config...
But you can create a second "server" section and configure another site (=document root)...

Related

How to limit amount of files sent to HTTP 2 / Server Push + Nginx

I'm configuring my server to work with HTTP 2 Server Push.
I could make it work, but or the browser or the nginx is limiting the amount of files to "push" in 10. I have a much bigger list of items (they are below).
Anyone knows if there is something in nginx that I need to config?
Chrome print below... Lok at the Initiator, only 10 items has the "push"
Chrome network image
My environment is:
3 Dockers containers.
1 php 7.3 container;
1 nginx 14.0.0 container;
1 mysql 8 container;
My nginx site config (ommited ssl certificate)
server{
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
client_max_body_size 4M;
include /etc/nginx/snippets/general-security-headers.conf;
location / {
try_files $uri \$uri /index.php?$args;
http2_push_preload on;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpserver;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
http2_push_preload on;
}
}
===== SOLVED ======
Edit: I found the config, it is the "http2_max_concurrent_pushes".
I added http2_max_concurrent_pushes 100;
in my nginx site config inside the "server" scope
I found the answer =D.
I found the config that i needed to change. It is the "http2_max_concurrent_pushes".
I added http2_max_concurrent_pushes 100; in my nginx site config inside the "server" scope

Why is my second site in nginx.conf not working?

I have a local NginX testing server on my Windows 10 machine. This is just for creating and testing websites, it is not served to the internet.
I've been testing one site successfully at localhost for a while, but now I want to add a second test site. I thought I could achieve this by duplicating the server{} block in the nginx.conf file and changing the name of the server_name and a few other parameters, but that it doesn't seem to work. When I try to load my second test site in Chrome, I get this error:
This site can’t be reached
local_test_2’s server DNS address could not be found.
My site at localhost still works, though.
Why is my second test site not working?
Here's my current nginx.conf file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type text/html;
sendfile on;
keepalive_timeout 65;
server {
#Server basics
server_name localhost;
listen 80;
index index.html index.php;
root c:/nginx/html;
location / {
try_files $uri $uri/ /index.php?_url=$uri&$query_string;
}
location ~ .(php|htm|html)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
server {
#Server basics
server_name local_test_2;
listen 80;
index index.html index.php;
root "C:\Users\User Name\Documents\Test\example.com";
location / {
try_files $uri $uri/ /index.php?_url=$uri&$query_string;
}
location ~ .(php|htm|html)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME c:/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}
}
}
Update:
My C:\Windows\System32\drivers\etc\hosts file has the following:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
The current 'localhost' specification is commented out. Should I change this file?
You need to add local_test_2 in your windows host file: at
C:\Windows\System32\drivers\etc\hosts
In host file add below line at the last
127.0.0.1 local_test_2
Also you can check reference to setup new host in nginx at: Setting up Nginx on local machine
The local_test_2 is a url that you created for testing purpose. Since you didn't buy it from some registrar, no DNS provider will be able to resolve the url to the ip address.
Every operating system has a hosts file(in linux it will be /etc/hosts) which can be used to map the urls to ip address without the use of some online DNS service. So in your case you can append the following line,
127.0.0.1 local_test_2
which tells to route all requests to local_test_2 to the same machine(127.0.0.1). No other changes are required in the hosts file.
Refer this link for more details on hosts files and different files used in different operating systems.

nginx => Serve two websites from one server block with sub-domain

I'd like to serve two applications from the same server through nginx. I'd like these applications to be available through a single domain name with sub-uris.
e.g.
www.example.com => should serve normal example site.
www.example.com/blog => blog.example.com (wordpress) site which is at different directory
Here, anything user requests from /blog/... should be served from blog site, but url should be www.example.com/blog/...
I have tried this.
server {
root /web/servers/example/public;
...
...
location /blog {
proxy_pass http://blog.example.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Other try was,
location /blog {
alias /web/servers/blog/public;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#allow 127.0.0.1;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_index index.php;
}
}
None of the options are working for /blog. Main site is working fine.
Could anybody please help me to solve the issue?

configuring nginx server_name if you don't have any domain name pointing to it

I have an ubuntu machine which has an ip address tied to it, and I have a site that i want to configure in my virtual host. the issue is what should I put in my server_name if I don't have any domain pointing to it? essentially I wanted such that when i enter my ip address 2xx.xxx.xxx.xxx then it goes to this site. Here's my current config
server {
listen 80;
server_name dev.somesite.com;
client_max_body_size 25M;
access_log /var/log/nginx/dev.somesite.com.access_log;
error_log /var/log/nginx/dev.somesite.com.error_log warn;
server_name_in_redirect off;
root /var/www/somesite-dev/web;
location / {
try_files $uri /app_dev.php?$args;
}
index app_dev.php index.php index.html;
fastcgi_index index.php;
location ~ \.php($|/) {
set $script $uri;
set $path_info "";
if ($uri ~ "^(.+\.php)(/.*)") {
set $script $1;
set $path_info $2;
}
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include /etc/nginx/fastcgi_params;
keepalive_timeout 0;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
Server_name is used for name based virtual hosting, if you don't need that, you can leave it out altogether. It will listen on IP just fine. Keep in mind that this will cause problems if you have more than one server that does not use name based resolution, you will only be able to access one of them. Which one depends on two things:
If you have "listen 80 default" in one of the servers, that server will take precedence
If you don't have the above, the server whose configuration is read first will handle the request
You should set server_name to e.g. development.local and then add a line to your /etc/hosts file when you make development.local to point to the IP of your server, or localhost.

nginx rewrite mystery - duplicating hostname and losing https

I am replacing lighttpd with nginx on my development server. I got it working with PHP and SSL, but I'm stumped by what should be a simple rewrite. I need to rewrite URLs from
http[s]://dev.foo.com/signup/123456
to
http[s]://dev.foo.com/signup/index.php?attcode=123456
The rule I am using is:
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
I have tried numerous variations on this, moved it around, put it inside a location block. What happens is the URL is rewritten to:
http://dev.foo.com/dev.foo.com/signup/123456
The hostname is inserted, and it seems to always lose https and go to http.
My nginx.com server section is below. I have read and re-read the nginx docs (as they are) and searched the nginx mailing list, but nothing I've tried has solved this problem.
Ubuntu 8.0.4 LTS in case that matters.
Thanks.
server {
listen 80;
listen 443 default ssl;
server_name dev.foo.com dev.bar.com localhost;
root /var/www/foo;
index index.php index.html;
# ssl cert stuff omitted
charset utf-8;
access_log /var/log/www/dev.access.log main;
location ~ /\. {
deny all;
}
location ~* ^.+\.(inc|tpl|sql|ini|bak|sh|cgi)$ {
deny all;
}
location ~* ^/(scripts|tmp|sql)/ {
deny all;
}
rewrite ^/robots.txt$ /robots_nocrawl.txt break;
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
location / {
try_files $uri $uri/ /error_404.php;
}
location ~ \.php$ {
fastcgi_pass localhost:51115;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SERVER_NAME $http_host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /error_404.php;
}
Don't put HTTP and HTTPS in the same server block. Separate them into two almost-identical server blocks, one for HTTP and one for HTTPS. Otherwise you will confuse all kinds of Nginx internals.

Resources