I want to deploy my laravel application with nginx using wildcard domain.
But that not work correctly. I have this error:
Corrupted Content Error
The site at http://www.exemple.com/ has experienced a network protocol violation that cannot be repaired.
The page you are trying to view cannot be shown because an error in the data transmission was detected.
Please contact the website owners to inform them of this problem.
An example for my laravel routing
<?php
Route::group(['domain' => "{sub}.exemple.com"], function() {
// load site content
});
This is my nginx configuration:
server {
# Update max body size
client_max_body_size 20M;
# SSL configuration
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server ipv6only=on;
ssl on;
include /etc/nginx/snippets/self-signed.conf;
include /etc/nginx/snippets/ssl-params.conf;
# Route and app index
root /var/www/site/public;
index index.php index.html;
# Make site accessible from https://www.exemple.com
server_name ~^([a-z]+)\.exemple\.com$;
location / {
if ($http_x_forwarded_proto != "https") {
return 301 https://$1.exemple.com$request_uri;
}
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SERVER_NAME $host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location ~* \.(?:ico|gif|jpe?g|png)$ {
expires 7d;
add_header Pragma public;
add_header Cache-Control "public";
access_log off;
}
location ~* \.(css|js|ttf)$ {
expires 1d;
access_log off;
add_header Cache-Control "public";
}
}
Related
I am building a docker collection that will eventually have the following containers (and more)
web (nginx)
proxy (reverse proxy nginx)
php-fpm
the web will have the allowance for several frameworks to be added via folders and subfolders
./folder1/folder2/codeigniter
./folder3/folder4/laravel
the folders for codeigniter and laravel are symlinks to a public folder
the index.php page works and shows the default routes without issue.
but when I try to get to a different page such as
/folder3/folder4/laravel/index.php/path/somwhere
I get a 404 error message.
I want to be able to do this without mapping an nginx location directive for EVERY FOLDER...
this is what my conf files looks like:
server {
listen 8100 default_server;
listen [::]:8100 default_server ipv6only=on;
server_name localhost;
root /var/www/public;
index index.php index.html index.htm;
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header "Access-Control-Allow-Headers" "Authorization, Origin, X-Requested-With, Content-Type, Accept";
add_header "Access-Control-Allow-Methods" "GET, POST, OPTIONS, HEAD";
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
return 204;
}
try_files $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri $uri/ index.php?$args;
fastcgi_pass php-fpm-56:9000;
fastcgi_index index.php;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fixes timeouts
fastcgi_read_timeout 600;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
what am I doing wrong here?
keep in mind, I want to use "/folder/index.php/path/path"
I DO NOT WANT
"/folder/path/path"
nor do I want to create a location entry for every new folder I create
i try to use codeifniter,
as an example
I will access
http://192.168.100.100/CI/CI_1/CI_1/index.php/main/index
after I tried this way, the results are according to what I attach
location /CI/CI_1/CI_1 {
autoindex on;
try_files $uri $uri/ /CI/CI_1/CI_1/index.php?/$request_uri;
}
Thanks
I am currently using a javascript framework being served up using Nginx, for example on the following url
www.myjsapp.com
I am also using Laravel 5.6 to build an API.
Instead of building 2 hosts, one for the JS app and one for Laravel, I want to be able to serve up the Laravel API on the following URL.
www.jsapp.com/api
Is this possible or do I have to always use 2 hosts?
The nginx server block for myjsappcom is as follows;
server {
listen 80;
listen 443 ssl http2;
server_name .myjsapp.com;
root "/home/project/myjsapp";
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/myjsapp.com-error.log error;
sendfile off;
client_max_body_size 100m;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
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/myjsapp.com.crt;
ssl_certificate_key /etc/nginx/ssl/myjsapp.com.key;
}
You can separate the servers using location blocks in your nginx config file:
different /location blocks will capture different url schemes and pass them to the respective servers (node or laravel).
server {
server_name mysjapp.com;
#other configurations like root, logs
location / {
#node server config
}
location /api {
#laravel server config
}
}
I have installed a orocrm on a AWS Lightsail VPS with Nginx & PHP7. orocrm installed without a hitch but I'm using Nginx for the first time and my virtual host doesn't seem to be working.
sites-available/default:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index app.php index.php index.html;
server_name 34.127.224.10;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I then purchased & pointed crmdomain.com to my instance and created another file.
sites-available/crm:
server {
listen 80;
server_name crmdomain.com www.crmdomain.com;
root /var/www/html/crm/web;
index app.php;
error_log /var/log/nginx/orocrm_error.log;
access_log /var/log/nginx/orocrm_access.log;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
location #rewrite { rewrite ^/(.*)$ /app.php/$1; }
location / {
try_files $uri /app.php$is_args$args;
}
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_index app.php;
fastcgi_read_timeout 10m;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I honestly don't know most of what is above, it's mostly based on tutorial and apache experience.
Errors
Domain based
- crmdomain.com -> 403 Forbidden nginx/1.10.0 (Ubuntu)
- crmdomain.com/app.php -> No input file specified.
- crmdomain.com/app_dev.php -> No input file specified.
- crmdomain.com/index.nginx-debian.html -> Welcome to nginx!
- crmdomain.com/user/login -> 404 Not Found nginx/1.10.0 (Ubuntu) **This is what should word**
Static IP based
- 34.127.224.10 -> 403 Forbidden nginx/1.10.0 (Ubuntu)
- 34.127.224.10/crm/web/ - No input file specified.
- 34.127.224.10/crm/web/app.php - No input file specified.
- 34.127.224.10/crm/web/app_dev.php - No input file specified.
- 34.127.224.10/index.nginx-debian.html -> Welcome to nginx!
- 34.127.224.10/crm/web/app.php/user/login -> 404 Not Found nginx/1.10.0 (Ubuntu) **This is what should word**
What am I doing wrong?
It's weird how often I answer my own questions:
I was able to change the default files and remove the extra virtual host. Since the server was only going to host the one app.
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 34.127.224.10 crmdomain.com www.crmdomain.com;
root /var/www/html/crm/web;
index app.php app_dev.php index.php;
location / {
# try to serve file directly, fallback to app.php
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config|install)\.php(/|$) {
#fastcgi_pass 127.0.0.1:9000;
# or
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS off;
}
error_log /var/log/nginx/orocrm_error.log;
access_log /var/log/nginx/orocrm_access.log;
}
I have a PHP website that uses subdomains for each user.
I want to be able to internally route cname.otherdomain.com to otherdomain.domain.com and pass this to PHP.
Currently I have this
server {
listen 80;
server_name cname.otherdomain.com;
rewrite ^ $scheme://otherdomain.domain.com$request_uri;
}
server {
listen 80 default_server;
server_name ~^(?<subdomain>[a-z\_\-\.]+)?\.?domain\.com$ "";
root g:/www;
index index.php;
#error_log /var/log/nginx/debug.log debug;
location ~ (?:application|modules|system) {
deny all;
}
location / {
try_files $uri $uri/ /index.php$request_uri;
}
location ^~ /index.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SERVER_NAME $http_host;
fastcgi_pass 127.0.0.1:9999;
}
}
If you want to hide this from the user, you need to keep it within a single server block. You could create a server block which captures the subdomain and reuses it with the fastcgi location block:
server {
server_name ~^cname\.(?<subdomain>.+)\.com$;
...
location ... {
fastcgi_param SERVER_NAME $subdomain.domain.com;
}
}
server {
listen 80 default_server;
...
}
Large amounts of duplication within nginx configuration files can be managed with include statements.
I would like to use dynamic host resolution with nginx and fastcgi_pass.
When fastcgi_pass $wphost:9000; is set in the conf then nginx displays the error
[error] 7#7: *1 wordpress.docker could not be resolved (3: Host not found),
but when I set fastcgi_pass wordpress.docker:9000;it is working except for the fact the that after a wordpress restart nginx still points to an old ip.
server {
listen [::]:80;
include /etc/nginx/ssl/ssl.conf;
server_name app.domain.*;
root /var/www/html;
index index.php index.html index.htm;
resolver 172.17.42.1 valid=60s;
resolver_timeout 3s;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args; ## First attempt to serve request as file, then as directory, then fall back to index.html
}
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
set $wphost wordpress.docker;
# pass the PHP scripts to FastCGI server listening on wordpress.docker
location ~ \.php$ {
client_max_body_size 25M;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass $wphost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
access_log off;
log_not_found off;
expires 168h;
}
# deny access to . files, for security
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
}
I have different virtual host configuration where I use proxy_pass http://$hostname; and in this setup everything is working as expected and the host is found.
After trying different options I wonder if fastcgi_pass does support variables
It does work if you pass the upstream as a variable instead, for example:
upstream fastcgi_backend1 {
server php_node1:9000;
}
upstream fastcgi_backend2 {
server php_node2:9000;
}
server {
listen 80;
server_name _;
set $FASTCGI_BACKEND1 fastcgi_backend1;
set $FASTCGI_BACKEND2 fastcgi_backend2;
location ~ ^/site1/index.php$ {
fastcgi_pass $FASTCGI_BACKEND1;
}
location ~ ^/site2/index.php$ {
fastcgi_pass $FASTCGI_BACKEND2;
}
}