A
A
AlexanderY2018-04-19 12:58:25
Nginx
AlexanderY, 2018-04-19 12:58:25

Is it possible to / give index.html for a request, and for other requests - to transfer processing further?

Is it possible to send the index.html file to nginx when requesting example.com, and when requesting example.com/anything-else, pass the request to php for processing?
PS Removed irrelevant directives from the config, like listen.

server {
  root /var/www/php/public; # Здесь находится index.php
  index index.php index.html index.htm index.nginx-debian.html;
  server_name localhost;
  
  location = / { # Здесь хочу отдать index.html
    root /var/www/static; # здесь физически находится index.html
    try_files $uri $uri/ /index.html?$query_string;
  }
  
  location / { # Здесь хочу обрабатывать остальные запросы, как обычно
    try_files $uri $uri/ /index.php?$query_string;
  }
}

With this configuration, the request for any url goes to index.php

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey, 2018-04-19
@AlexanderY

In the first case, you do not need to do try_files $uri.
You need to try_files / index.html
That is, the config should look like this:

server {
  root /var/www/php/public; # Здесь находится index.php
  index index.php index.html index.htm index.nginx-debian.html;
  server_name localhost;
  
  location = / { # Здесь хочу отдать index.html
    root /var/www/static; # здесь физически находится index.html
    try_files /index.html =404;
  }
  
  location / { # Здесь хочу обрабатывать остальные запросы, как обычно
    try_files $uri $uri/ /index.php?$query_string;
  }
}

K
key don, 2018-04-19
@keydon2

What is wrong with replacing
location = / { # Here I want to give index.html
to
location /index\.html$ {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question