ashokvishnu's avatar

Configuring nginx on windows

Has anyone tried configuring nginx on windows for executing laravel? Below is my server configuration, which is not working. server { listen 80 default_server; listen [::]:80 default_server; server_name _; root D:\Softwares\nginx-1.15.5\html\myweb\public; index index.php index.html;

    location / {
location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    }
    try_files $uri $uri/ /index.php?$query_string;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

Any working configuration or suggestions?

0 likes
9 replies
lostdreamer_nl's avatar

for developing laravel within windows, I'd suggest installing laragon.

https://laragon.org/

This will install (locally or on a USB) :

  • nginx or apache with auto domains (vhost and windows host file)
  • PHP
  • Python
  • MySQL
  • PostgreSQL
  • MongoDB
  • redis
  • memcached

easy install, and every subfolder you create in your www folder will automatically become subfolder.test for your development domain (even includes auto SSL via letsencrypt)

1 like
MarkLL's avatar

Hey @ashokvishnu You conf is not formatted very well but it looks like you have a location within a location. Also where is the fastcgi_pass statement?

I set up all my dev sites in their own conf file on my windows machine. The main nginx.conf file contains include ../../vhosts/*.conf; at the bottom (before the last closing brace for the html{} section) to load all my sites. Each one is similar to the following:

server {
    listen       80;
    server_name  marktest.local
    root D:/dev/marktest/public;
    access_log  logs/$host.access.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Standard PHP to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_buffering on;
      
      include fastcgi.conf;
    }
}

Here is an example of the batch file I use to start it all.

start-nginx-php.cmd

echo Starting nginx config (includes PHP 7.2 FastCGI)...
set phpPath=D:\wamp\bin\php\php7.2.4nts
set nginxPath=D:\wamp\bin\nginx\nginx1.13.9

start /D "%phpPath%" /MIN php-cgi.exe -b 127.0.0.1:9000 -c %phpPath%\php.ini

timeout /t 1
start /D "%nginxPath%" nginx.exe
echo.
echo Done...

Hope that helps.

ashokvishnu's avatar

@MarkLL Thanks for the reply. I tried the same configuration but it seems laravel is not working. When I try rest get/post request I am getting the response as "No input file specified" any guess?

ashokvishnu's avatar

Tried creating php file but got the same result as before. Just giving localhost gives the Nginx Welcome page, so I doubt whether the root is working at all? Here is the nginx.conf

#user nobody; worker_processes 1;

#error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info;

#pid logs/nginx.pid;

events { worker_connections 1024; }

http { include mime.types; default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  localhost
    root D:/Softwares/nginx-1.15.5/html/erp/public;
    access_log  logs/$host.access.log;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Standard PHP to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_buffering on;
      
      include fastcgi.conf;
    }
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

}

rawilk's avatar

@ashokvishnu - As @lostdreamer_nl had stated before, use Laragon. It's very easy to setup for windows and has a lot of the things you need for a server. There, now you have a solution.

ashokvishnu's avatar

@wilk_randall - The issue is that I am not able to get laravel working in nginx. The same issue is there whether i use nginx directly or nginx inside laragon.

lostdreamer_nl's avatar

strange, laragon should be setting up nginx for you automatically.

You could try the following though..... remove all your sites-enabled from your nginx setup and only include the following as a default:

server {
        listen 80;
        server_name ~^(.*)\.test;
        set $domain $1;

        root D:/Softwares/nginx-1.15.5/html/$domain/public;
        access_log  logs/$domain.access.log;

        index index.html index.php;

        # Media: images, icons, video, audio, HTC
        location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
          expires 1M;
          access_log off;
          add_header Cache-Control "public";
        }

        # CSS and Javascript
       location ~* \.(?:css|js|woff|woff2|eot|)$ {
         expires 1y;
         access_log off;
         add_header Cache-Control "public";
       }

        location / {
                try_files $uri $uri/ =404 /index.php?$query_string;
        }
        location ~ \.php$ {
                include fastcgi.conf;

                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_buffering on;
        }
}

This would give any domain ending in .test a root in "D:/Softwares/nginx-1.15.5/html/{$domainName}/public"

so if you add this to your windows hosts file:

127.0.0.1   erp.test
127.0.0.1   develop.test

going to http://erp.test would end you in your nginx in the folder "D:/Softwares/nginx-1.15.5/html/erp/public"

same for develop.test: "D:/Softwares/nginx-1.15.5/html/develop/public"

If that also does not work, I really suggest removing this nginx installation and re-trying laragon.

Please or to participate in this conversation.