Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


nGINX Configuration for custom PHP Framework
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

nGINX Configuration for custom PHP Framework

BharatBBharatB Member, Patron Provider

So I'm having a hard time with nginx rewrite wondering if anyone could help so this is the situation

Directory Structure




base .htaccess




public .htaccess

How do I write my nGINX config so that the above works :/

«1

Comments

  • joepie91joepie91 Member, Patron Provider

    You're probably looking for the try_files directive. You don't need mod_rewrite for it.

    Thanked by 1BharatB
  • BharatBBharatB Member, Patron Provider

    Tried that not able to figure it out.

  • DerekDerek Member
    edited September 2015

    the first example just rewrites everything to /public/, so when you configure nginx, just set the root /path/to/public/

    this is the second one that should be

    rewrite ^/(.)$ /index.php break;

    So all together, after server{ should look like;


    location / {
    root /path/to/public;
    rewrite ^/(.)$ /index.php break;
    location ~* ^.+.(jpg|jpeg|gif|css|js)$ {
    root /path/to/public;
    }
    }

    Edit: This isn't formatting nicely above, Pastebin: http://pastebin.com/YNHVQ4Kb

    Thanked by 1BharatB
  • Like others have said, you really just need to set the public directory as your root. Then use: try_files $uri $uri/ index.php;

  • JustAMacUserJustAMacUser Member
    edited September 2015

    e: duplicate post

  • BharatBBharatB Member, Patron Provider

    I got the first part solved only 1 thing is left im not able to access assets folder in public folder for some reason its throwing a 404 error.

  • BharatBBharatB Member, Patron Provider
    edited September 2015


    server {
    listen 80;
    server_name some.domain;
    root /usr/share/nginx/html/public;
    index index.php;
    location / {
    rewrite ^/(.)$ /index.php break;
    include fastcgi_params;
    fastcgi_intercept_errors off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location /assets {
    try_files $uri $uri/ $document_root;
    }
    }

    Edit: Solved it. So now please tell me if the above way is correct or if I should make any changes.

  • SolusVMSolusVM Member, Host Rep

    If all you're even going to do is re-direct to index.php in that location block then maybe get rid of the rewrite?

    Also you could configure FPM to listen on a socket.

    location / { include fastcgi_params; fastcgi_intercept_errors off; fastcgi_pass /path/to/unix/socket; fastcgi_param SCRIPT_FILENAME /path/to/index.php; }

    Thanked by 1BharatB
  • BharatBBharatB Member, Patron Provider

    @SolusVM said:
    If all you're even going to do is re-direct to index.php in that location block then maybe get rid of the rewrite?

    Since you're here could you tell why nginx can't open ports above 1024 ? :/

  • SolusVMSolusVM Member, Host Rep

    Not sure on that one. It usually can. Anything in the logs etc? Firewall, selinux?

  • 0xdragon0xdragon Member
    edited September 2015

    @BharatB said:
    Since you're here could you tell why nginx can't open ports above 1024 ? :/

    You mean below 1024, right? That's because you need elevated (read: root) permissions in order to open a port lower than that.

    http://serverfault.com/questions/112795/how-can-i-run-a-server-on-linux-on-port-80-as-a-normal-user

  • @JustAMacUser said:
    Like others have said, you really just need to set the public directory as your root. Then use: try_files $uri $uri/ index.php;

    This is the most simple option.

  • BharatBBharatB Member, Patron Provider

    @0xdragon said:

    ill check

  • BharatBBharatB Member, Patron Provider
    edited September 2015

    My problem is quite opposite to that post he's not able to open anything except port 80 81 or 8080 for nginx

  • @BharatB said:
    My problem is quite opposite to that post he's not able to open anything below 1024 and im not able to open anything above 1024 :/

    That's err.. Odd.. Are you sure the port you're trying to bind to isn't occupied by an existing service or blocked by your firewall?

  • BharatBBharatB Member, Patron Provider

    @0xdragon said:
    That's err.. Odd.. Are you sure the port you're trying to bind to isn't occupied by an existing service or blocked by your firewall?

    iptables turned off all ports freed idk what the fuck is wrong and beating my head for past few hours ( successfully introduced phpseclib into the framework in meantime ).

  • @BharatB said:

    Would you mind pastebinning your Nginx config? :)

  • BharatBBharatB Member, Patron Provider


    server {
    listen 80;
    server_name rcp.readydedi.com;
    root /usr/share/nginx/html/public;
    error_log /var/log/nginx/php_error.log error;
    index index.php;
    location / {
    rewrite ^/(.)$ /index.php break;
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_intercept_errors off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location /assets {
    try_files $uri $uri/ $document_root;
    }
    }


    server {
    listen 81;
    server_name default_server;
    root /usr/share/nginx/html/public/phpma;
    index index.php;
    location / {
    include /etc/nginx/fastcgi_params;
    fastcgi_index index.php;
    fastcgi_intercept_errors off;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location /themes {
    try_files $uri $uri/ $document_root;
    }
    }

    @0xdragon said:

  • @BharatB said:

    What was the error you were hit with?

  • BharatBBharatB Member, Patron Provider

    @0xdragon said:

    When I change port to anything for eg 2081 or 2082 or any other port says nginx bind failed and error is permission denied

  • SELinux?

  • BharatBBharatB Member, Patron Provider

    @wych said:
    SELinux?

    Disabled, setenforce=0 also done

  • BharatBBharatB Member, Patron Provider

    So anyone else can suggest anything regarding to ports issue :/

  • netstat -plnt | grep PORTNUMBERHERE

    Try this one. Never had any problems with assigning ports to nginx.

    Try and see if there's an app using that port.

  • BharatBBharatB Member, Patron Provider

    @Nomad said:
    Try and see if there's an app using that port.

    I've already checked :/ theres no app using the port im trying to assign, check this

    2015/09/27 04:41:07 [emerg] 3781#0: bind() to 0.0.0.0:2080 failed (13: Permission denied)

  • edited September 2015

    BharatB said: I've already checked :/ theres no app using the port im trying to assign, check this

    2015/09/27 04:41:07 [emerg] 3781#0: bind() to 0.0.0.0:2080 failed (13: Permission denied)

    Maybe this can help: http://serverfault.com/a/566320

    -

    If the above doesn't help, try setting CAP_NET_BIND_SERVICE to the nginx binary, something like:

    $ sudo setcap 'cap_net_bind_service=+ep' /usr/bin/nginx

    (fix the nginx path, naturally).

    Edit #1: fixed the link.

    Edit #2: added info about setcap

  • Try not use "default_server"

  • BharatBBharatB Member, Patron Provider

    @rokok said:
    Try not use "default_server"

    Yea I know its just home server so just used it so I can access over ip also.

  • On some nginx mainline version, the "default_server" can cause problem if you have multiple blocks or conflict with other nginx module you compile from scratch, you need to understand the server order if use default_server.

    If that happened in your case, all traffic will through from port 81

Sign In or Register to comment.