10 months ago
I'm creating a native php project where I used XAMPP with the .htaccess file to redirect requests but here nginx is used, so how can I set the nginx.cof file to redirect it?
13 Replies
10 months ago
Hey Mdriod,
Usually there is a main nginx.conf
file or a specific virtual host file and then you can modify that file with your redirection rules. This is a sample file, your mileage may vary.
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
# Redirect all HTTP traffic to HTTPS
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name yourdomain.com www.yourdomain.com;
# SSL configuration
# Root directory and index file
root /var/www/yourproject;
index index.php index.html;
# Redirect specific paths
location /old-path {
return 301 /new-path;
}
location /another-old-path {
return 301 /another-new-path;
}
# PHP-FPM configuration
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; # Adjust PHP version
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
9 months ago
mmm isn't it with the nginx.template.conf file in the root directory?
9 months ago
template isn't used by NGINX, it's there as a base line, you will need to duplicate and rename it.
9 months ago
Ahh I understand, so the nginx.conf file, do I have to generate it somewhere or is it already generated to set it up?
9 months ago
Angelo is slightly mistaken here, you do indeed wan't to have a nginx.template.conf
file in the root directory if that is the route you want to go.
This is the default file in use by nixpacks -
https://github.com/railwayapp/nixpacks/blob/main/src/providers/php/nginx.template.conf
You can download that, place it in the root directory and modify it to your needs and it will be used instead of the default.
It's ran through a pre-run script that does variable expansion during runtime before it's passed to nginx.
Or, without having to modify a config file, you can try setting NIXPACKS_PHP_FALLBACK_PATH
to /index.php
Nixpacks php docs -
9 months ago
I did as he told me, downloaded the nginx.template.conf
and renamed it nginx.conf
and put it at the root of my project. Now he's breaking my container with this mistake:
nginx: [emerg] directive "include" is not terminated by ";" in /app/nginx.conf:11
But it's supposed to put the semicolon and aside it's a copy and paste.
9 months ago
Sorry for not being clear, but you do not want to rename that file, the filename should stay as nginx.template.conf
9 months ago
Without needing any config files, have you tried my other proposed solution of setting the NIXPACKS_PHP_FALLBACK_PATH
variable?
9 months ago
XAMPP uses Apache and not Nginx, so you need to use an .htaccess file to redirect traffic. You should implement an image that uses the same development server, in this case, Apache, as Nginx handles traffic differently. Here is an example of an .htaccess file that redirects all traffic:
.htaccess save in to root app
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
example dockerfile for apache same for xampp
FROM php:8.1-apache
# Copy your code to the web server root directory
COPY . /var/www/html/
# Enable necessary modules
RUN a2enmod rewrite
# Set the environment
ENV APACHE_DOCUMENT_ROOT /var/www/html
# Expose port 80
EXPOSE 80
# Start Apache in the foreground
CMD ["apache2-foreground"]
9 months ago
Sorry for not being clear, but you do not want to rename that file, the filename should stay as
nginx.template.conf
Thank you all for the help I have already managed to make the redirect correctly. The solution was as @Brody said, not to change the file's name to nginx.conf and with that it worked.
Status changed to Solved railway[bot] • 10 months ago
9 months ago
Without needing any config files, have you tried my other proposed solution of setting the
NIXPACKS_PHP_FALLBACK_PATH
variable?
No, it's not a thing. I haven't used that because it wasn't used and in documentation it doesn't give many examples to understand them either. I also wanted to change the PHP version but I didn't quite understand it.
9 months ago
No, it's not a thing. I haven't used that because it wasn't used and in documentation it doesn't give many examples to understand them either.
I assure you it is a thing, the docs for the php provider does explain the variable and gives a usecase and example, please see docs page -https://nixpacks.com/docs/providers/php
But if you got it working how you want with a config then that's just as good!
I also wanted to change the PHP version but I didn't quite understand it.
The version of php that is used is based on what you have set in your composer.json file.