proxy_pass priority and location - nginx

I am trying to proxy all incoming requests from nginx to NodeJS, excluding uri "/cabinet/...". So according to official nginx documentation, it's work perfectly when I am trying to proceed file on same server, but when I am trying proxy to node server, whole request are forced to node (including /cabinet/)
So what I am doing wrong?
server {
listen 80;
listen 443 ssl http2;
server_name mysite.dev;
root "/home/vagrant/code/cabinet/public";
index index.html index.htm index.php;
charset utf-8;
location / {
proxy_pass http://localhost:5000;
#try_files $uri $uri/ /index.html;
}
location /cabinet/ {
#try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index2.html;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/mysite.dev-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/mysite.dev.crt;
ssl_certificate_key /etc/nginx/ssl/mysite.dev.key;
}

Related

How to configure nginx correct for both laravel and yii project?

Im trying to configure nginx to serve laravel and yii project on same domain. My laravel project is working fine. Yii project also working but assets folder on yii project is giving err_aborted not found 404. All js css ... files are not found
server {
server_name mydomain.com;
index index.html index.php;
charset utf-8;
set $base_root /var/www;
# App 1 (main app)
location / {
root $base_root/telemele/public;
try_files $uri $uri/ /index.php?$query_string;
error_log /var/log/nginx/telemele.notice.log notice;
error_log /var/log/nginx/telemele.error.log error;
location ~* ^/index\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/telemele/public/index.php;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
}
# App 2
location ~* /mahabat {
alias $base_root/html/backend/web;
try_files $uri $uri/ /mahabat/index.php?$query_string;
error_log /var/log/nginx/mahabat.notice.log notice;
error_log /var/log/nginx/mahabat.error.log error;
location ~* ^/mahabat/index\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/html/backend/web/index.php;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~* \.css|\.js|\.jpg|\.jpeg|\.png|\.gif|\.swf|\.svg|\.tiff|\.pdf$ {
try_files $uri =404;
}
location ~ ^/assets/.+\.php(/|$) {
deny all;
}
}
# Files
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
# Error
access_log off;
rewrite_log on;
# Disable .htaccess access
location ~ /\.ht {
deny all;
}
}
What am i doing wrong? How to make nginx not to abort assets folder files? why it is giving not found?
There might be a missing .htacces file at the frontend/web directory. If you have the same problem, I recommend you to use this

Bolt CMS on Vagrant does not parse PHP

My vagrant box doesn't parse PHP-files for Bolt CMS.
I'm a dev for years now, been working in Vagrant since about 5 years, and never had serious problems.
I wanted to give Bolt CMS a try, but when I fire my browser to the correct url (http://sallys.local:8000) it always wants to download the index-file (or any other file) instead of parsing it.
My vagrant-box is updated to the latest version 8.10, I use Nginx, but it seems as Nginx isn't called. I activated the acces-log and it shows no entries. For my other projects, same box, it does.
The Nginx-config for this one is:
server {
listen 80;
listen 443 ssl;
server_name sallys.local;
root "/home/vagrant/sallys/public";
index index.html index.htm index.php app.php;
charset utf-8;
location / {
try_files $uri $uri/ /app.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
# access_log off;
access_log /var/log/nginx/sallys.local-ssl-acces.log;
error_log /var/log/nginx/sallys.local-ssl-error.log error;
sendfile off;
client_max_body_size 100m;
# DEV
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
# Prevents URIs that include the front controller. This will 404:
# http://domain.tld/app.php/some-path
# Remove the internal directive to allow URIs like this
internal;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/sallys.local.crt;
ssl_certificate_key /etc/nginx/ssl/sallys.local.key;
}
I have a similar setup for my other projects, and they all respond on port 8000. And parse PHP-files correctly. This seems to be a noob-problem. But I can't find the problem in here.
when I'm using the built-in server, it does work. So there must be a problem with Nginx.
Any one any idea?
Thanks
Tim
It looks like that is an Nginx site config for a Symfony 2/3 set-up (Symfony 4 is simpler FWIW), so it won't work "out of the box", so to speak.
My guess would be the location / {} as that is referencing app.php, and by default, a Bolt install will use index.php as the index file in the webroot.
There is a specific documentation page in Bolt's documentation that covers Nginx configuration specifically for a Bolt install, and I'd guess that'd be enough for your use case.
Thanks for pointing me out, Gawain. I tried your solution, but it didn't work. I removed the entries in my homestead file, reprovisioned vagrant, ..... and it works...
There is a new config generated:
server {
listen 80;
listen 443 ssl http2;
server_name sallys.local;
root "/home/vagrant/sallys/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/sallys.local-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/sallys.local.crt;
ssl_certificate_key /etc/nginx/ssl/sallys.local.key;
}

redirecting to root, even if query is empty

All my links is redirecting to root, where I serve the file "index.php".
This is my nginx config :
/etc/nginx/sites-available/myproject.local
server {
listen 80;
listen 444 ssl http2;
server_name .buildurlshortener.local;
root "/home/vagrant/codecourse/buildurlshortener/public";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/buildurlshortener.local-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ /\.ht {
deny all;
}
ssl_certificate /etc/nginx/ssl/buildurlshortener.local.crt;
ssl_certificate_key /etc/nginx/ssl/buildurlshortener.local.key;
}
If I make a post request to "http://myproject.local/something", it works.
But a post request to the root, i.e. http://myproject.local, is not working.
I get "405 Not Allowed" from nginx.
If I add a rule with "location ~ { ... }", then I can post to "http://myproject.local". But now it is "http://myproject.local/something" that is not working.
How can I also serve "index.php" from the root ("/"), without breaking my other routes ?
One of the causes of message "405 Not Allowed" is that nginx can't serve static content on POST-request. Could you show config more detailed?
To this line :
location ~ .php$
I appended |/$ :
location ~ .php$|/$
So that it can also accept an empty query.
Now I can use post requests to "myproject.local", "myproject.local/index.php", ""myproject.local/someroute".

Nginx server multiple site and domain redirection (on same port/different port)

I am trying to host multiple unrelated sites on the same nginx server. How do I edit the http.conf and the https.conf file in order to make it work?
ORIGINAL HTTP.CONF
server {
listen 2333;
server_name port1.example.com;
rewrite ^/(.\*) http://port1.example.com/$1 permanent;
root /var/www/html;
index index.php index.html;
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/http_access.log combined;
error_log /var/log/nginx/http_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
}
ORIGINAL HTTPS.CONF
server {
listen 4433 ssl default_server;
server_name _;
ssl_certificate /etc/nginx/ssl/port2.example.com.crt;
ssl_certificate_key /etc/nginx/ssl/port2.example.com.key;
root /usr/share/nginx/html;
index index.php index.html;
# set max upload size
client_max_body_size 2G;
fastcgi_buffers 64 4K;
access_log /var/log/nginx/https_access.log combined;
error_log /var/log/nginx/https_error.log;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ \.php$
{
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~* \.(htaccess|htpasswd) {
deny all;
}
# set long EXPIRES header on static assets
location ~* \.(?:jpg|jpeg|gif|bmp|ico|png|css|js|swf)$ {
expires 30d;
access_log off;
}
}
My port 4433 is on SSL obviously. How can I get port 2333 to work only on port1.example.com and port 4433 to work only on port2.example.com It's better if 2333 can be served on SSL, too.
Thanks guys for viewing. I ended up using Nginx and it was way faster setting up using multiple .conf files in the /etc/nginx/conf folder.

Laravel 5 - NGINX Server Config Issue with URL Query Strings

Laravel is not receiving any $_GET variables from the URL query string. The $_GET and Input::all() are empty.
Example:
example.app/ex/login.php?country=US
The "country=US" never shows up in my $_GET variable
After much research and trying many different NGINX configurations, I can now only produce results when this example is used.
Example:
example.app/index.php/ex/login.php?country=US
The $_GET variable now shows the country name value pair.
What is the proper configuration to allow query strings within the URL?
My current sites-enabled configuration for my "example.app" is...
server {
listen 80;
server_name example.app;
root /home/vagrant/Code/public;
index index.html index.htm index.php;
charset utf-8;
location / {
#try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/registration.app-error.log error;
error_page 404 /index.php;
sendfile off;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
location ~ /\.ht {
deny all;
}
}
This appears correct, aside from being commented out.
location / {
#try_files $uri $uri/ /index.php?$query_string;
}
The bare bones one I use in testing is:
server {
listen 80 default_server;
root /var/www/project/public;
index index.php
server_name localhost;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}
$query_string and $args are functionally identical according to:
NGINX Docs
This is the sites-enabled NGINX server configuration that ended up working for me...
server {
listen 80;
server_name registration.app;
root /home/vagrant/Code/registration/public;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
access_log off;
error_log /var/log/nginx/registration.app-error.log error;
error_page 404 /index.php;
sendfile off;
# Point index to the Laravel front controller.
index index.php;
location / {
try_files $uri $uri/ index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
#deny all;
}
}
For me it worked only by changing
try_files $uri $uri/ /index.php?$query_string; to
try_files $uri $uri/ /index.php?$args inside location / block on ubuntu (14.04) nginx
My NGINX congfiguration was something like this:-
From
location / {
try_files $uri $uri/ /index.php?is_args$args;
}
To
location / {
try_files $uri $uri/ /index.php?$args;
}
I just remove the is_args from try_files and working fine.

Resources