I have next conf file. / points to SPA and /api to my backend server. now i need to set static files. so i do.
location / {
root /var/www/prod2020/dist;
}
location /js {
root /var/www/new-rte2020/public/js;
}
location /images {
root /var/www/new-rte2020/public/images ;
}
location /css {
root /var/www/new-rte2020/public/css ;
}
But this doesnt seems to work, im getting 404 from nginx. How can i make it work. in a next way:
https://mysite/ - /var/www/prod2020/dist - works
https://mysite/js - /var/www/new-rte2020/public/js - doen't work
https://mysite/js/app.js - /var/www/new-rte2020/public/js/app.js - doen't work
https://mysite/js/widget/w.js - /var/www/new-rte2020/public/js/widget/w.js - doen't work
Here is almost full conf file
#server {
# if ($host = a.rte.im) {
# return 301 https://$host$request_uri;
# }
# listen 80;
# listen [::]:80;
# server_name a.rte.im;
# return 404; # managed by Certbot
#}
server {
# listen 443;
# ssl on;
# ssl_certificate /etc/letsencrypt/live/s.rte.im/fullchain.pem;
# ssl_certificate_key /etc/letsencrypt/live/s.rte.im/privkey.pem;
# root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name a.rte.im;
location ~ /js {
root /var/www/new-rte2020/public/js;
}
location ~ /images {
root /var/www/new-rte2020/public/images ;
}
location ~ /css {
root /var/www/new-rte2020/public/css ;
try_files $uri /var/www/new-rte2020/public/css$uri;
}
location /api {
root /var/www/new-rte2020/public;
try_files $uri /index.php?$query_string;
}
location /chatify {
root /var/www/new-rte2020/public;
try_files $uri /index.php?$query_string;
}
location /storage {
alias /var/www/new-rte2020/storage/app/public;
}
location /payment-form {
root /var/www/new-rte2020/public;
try_files $uri /index.php?$query_string;
}
location /upgrades/payment-form {
root /var/www/new-rte2020/public;
try_files $uri /index.php?$query_string;
}
location /test {
root /var/www/new-rte2020/public;
try_files $uri /index.php?$query_string;
}
location / {
root /var/www/prod2020/dist;
}
location ^~ /assets/ {
include /etc/nginx/mime.types;
root /var/www/prod2020/dist;
}
location ~ \.php$ {
root /var/www/new-rte2020/public;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 405 =200 $uri;
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/a.rte.im/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/a.rte.im/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = a.rte.im) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
listen [::]:80;
server_name a.rte.im;
return 404; # managed by Certbot
}
Related
We have a website where we are using a Laravel for the part available to the end-user, and an older legacy codebase for the admin panel.
Currently root is the public folder for the currently deployed version.
The website folder structure looks something like this:
admin/
|____index.php <-- desired entrypoint for admin-related requests
app/
public/
|____index.php <-- main entrypoint for website
resources/
routes/
So, when someone wants to access the admin panel they go to example.com/admin
Here's the current nginx configs file we are using.
example.com
server {
server_name example.com;
root /var/www/example.com/current/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /admin {
proxy_pass http://localhost:3000;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
example.com/admin
server {
listen 3000;
server_name example.com;
root /var/www/example.com/current/admin;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index 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; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I'm not very well versed in nginx server configuration and would love som assistance on this.
EDIT 1
Config file using map:
EDIT 2
Updated the config below with the changes made to make it work.
map $uri $siteroot {
# This didn't work, per the accepted answer.
# ^/admin /var/www/example.com/current/admin;
# This works great!
^/admin /var/www/example.com/current;
default /var/www/example.com/current/public;
}
server {
server_name example.com;
root $siteroot;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name example.com;
return 404; # managed by Certbot
}
If you try changing root directory, Nginx will shows not found error.
Because, Nginx tries find file with request path even you changed root.
For example, If you change root directory for /admin path to /app/admin, Nginx will find file in /app/admin/admin. That's why you can't reach file.
Try insert rewrite /admin(.*) $1 break; line at /admin location block and set root path, or use alias expression.
I would use map directive for this, something like
map $uri $siteroot {
~^/admin /var/www/example.com/current/admin;
default /var/www/example.com/current/public;
}
server {
server_name example.com;
root $siteroot;
...
}
I have two laravel apps like this:
- apps
- dashboard
- website
I'm trying to serve them on linux machine, I can't seem to get them to work together.
I want to have two urls like this:
https://test.example.co
https://test.example.co/dashboard
server {
server_name test.example.co;
index index.php index.html index.htm index.nginx-debian.html;
location = / {
root /var/www/example/apps/website;
try_files $uri $uri/ /public/index.php;
}
location ~ ^/dashboard {
root /var/www/example/apps;
try_files $uri $uri/ /dashboard/public/index.php;
#try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/test.example.co/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/test.example.co/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
# pass the PHP scripts to FastCGI server listening on /var/run/php7.4-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
if ($host = test.example.co) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
can you tell me what is the result if you write your code like this:
location ~ ^/dashboard {
root /var/www/example/apps/dashboard/public;
try_files $uri $uri/ /index.php;
}
location = ~ ^/ {
root /var/www/example/apps/website/public;
try_files $uri $uri/ /index.php;
}
I just swap it. so it will search for the dashboard and if the user is not in dashboard, it will check the root.
I succeeded serving them using another subdomain, the "/dashboard" idea didn't work.
I hope someone can suggest the correct way to do it, I would change the accepted answer.
I have now example.com and admin.example.com.
Nginx config as below:
server {
server_name example.com;
index index.php index.html index.htm index.nginx-debian.html;
root /var/www/example/apps/website/public;
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log;
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
# pass the PHP scripts to FastCGI server listening on /var/run/php7.4-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com;
return 404; # managed by Certbot
}
And the second one is:
server {
server_name admin.example.com;
index index.php index.html index.htm index.nginx-debian.html;
root /var/www/example/apps/dashboard/public;
try_files $uri $uri/ /index.php$is_args$args;
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php7.4-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/admin.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/admin.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = admin.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
return 404; # managed by Certbot
}
I am trying to exclude path client and every url that contains client from https, using nginx.
server {
listen 80;
server_name example.com www.example.com;
location / {
return 301 https://www.example.com$request_uri;
}
location /client/ {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443 ssl;
server_name example.com www.example.com;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
root /var/www/laravel/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location /client/ {
return 301 http://www.example.com$request_uri;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
location ~ /.well-known {
allow all;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires 30d;
add_header Vary Accept-Encoding;
access_log off;
}
}
I removed ssl certificate key from second server block.
When i type https://example.com/client, i get redirected to http://www.example.com/client (as expected). I have a simple index.php file inside client folder that is not getting executed. I am seeing too many redirects in browser.
Credit to https://stackoverflow.com/users/4862445/richard-smith
Added root and index statements in first server block.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/laravel/public; #LINE-ADDED
index index.php index.html index.htm; #LINE-ADDED
# other code here ...
}
I have configured http->https redirection + non-www->www redirection.
I want to exclude two paths, so that they won't be redirected to https.
I have tried many possible configuration, I either get a 404 or I get redirected to the https version.
Here's the current config, which returns a 404, when trying to get a /loc2/ path (#curl http://www.server.dev/loc2/18a9BM4Lay):
server {
listen 80;
listen [::]:80;
server_name server.dev;
location / {
return 301 https://$server_name$request_uri;
}
location /loc1/ {
try_files $uri $uri/ /index.php?$args;
}
location /loc2/ {
try_files $uri $uri/ /index.php?$args;
}
}
server {
listen 80;
listen [::]:80;
server_name www.server.dev;
root /var/www/web/server/public;
location / {
# return 301 https://$server_name$request_uri;
}
location ^~ /loc1/ {
# root /var/www/web/server/public;
index index.php;
# try_files $uri $uri/ /index.php?$args;
include pool_web.conf;
}
location ^~ /loc2/ {
# root /var/www/web/server/public;
index index.php;
# try_files $uri $uri/ /index.php?$args;
location ~ \.php$ {
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
include fastcgi.conf;
fastcgi_read_timeout 360s;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/server-php7.0-fpm.sock;
}
# include pool_web.conf;
}
}
server {
# listen 80;
# listen [::]:80;
listen 443 ssl http2;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
server_name server.dev;
rewrite ^ $scheme://www.server.dev$request_uri? permanent;
}
server {
# listen 80;
listen 443 ssl http2;
ssl_certificate /etc/ssl/server.crt;
ssl_certificate_key /etc/ssl/server.key;
server_name www.server.dev;
root /var/www/web/server/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location /images/ {
try_files $uri =404;
}
location ~ \.php$ {
include pool_web.conf;
}
location ~ \.(css|htc|less|js|js2|js3|js4)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|woff|xla|xls|xlsx|xlt|xlw|zip)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
}
}
You need to add a try_files statement to define a default handler. The index directive only works if a directory is specified.
For example:
location ^~ /loc2/ {
try_files $uri $uri/ /loc2/index.php;
...
}
See this document for details.
i have a nginx server running and want to remove the .php extension from my files. I have allready tried a few things but the only thing i managed to accopmplish was breaking the fastcgi proccessing leading into downloading php files. The server is running fine with the following configuration:
##
# Virtual Host configuration for example.com
##
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GC$
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/letsencrypt/dhparams.pem;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /usr/share/nginx/html/example/;
index index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
location /uploads {
deny all;
}
error_page 404 /templates/404.php;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_intercept_errors on;
}
location ~* \.(?:ttf|ttc|otf|eot|woff|font.css|jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
access_log off;
add_header Cache-Control "public";
}
location ~ /\. {
deny all;
}
}
Thank you for your effort and time.
##
# Virtual Host configuration for example.com
##
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GC$
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/letsencrypt/dhparams.pem;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /usr/share/nginx/html/example/;
index index.php;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ #extensionless-php; // add #extensionless-php
}
location /uploads {
deny all;
}
error_page 404 /templates/404.php;
location ~ \.php$ {
try_files $uri =404; // add this
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_intercept_errors on;
}
location #extensionless-php { // add this block
rewrite ^(.*)$ $1.php last;
}
location ~* \.(?:ttf|ttc|otf|eot|woff|font.css|jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
access_log off;
add_header Cache-Control "public";
}
location ~ /\. {
deny all;
}
}
from this site http://www.tweaktalk.net/60/nginx-remove-php-file-extension-from-url