October 22, 2024
Chicago 12, Melborne City, USA
PHP

Rust “hello world” script not running via ajax calls, but is via curl in the server itself


I have a PHP web application using Apache. I’m trying to add the ability to run Rust scripts.

I can get it to work on localhost via:

fetch('/rust/hello')  // Proxy through Apache (no need for port in URL)
    .then(response => response.text())
    .then(data => {
        console.log(data);  // Should log "Hello, world!"
    })
    .catch(error => console.error('Error:', error));

It also works on production via CLI command: curl http://localhost:8080/hello

I cannot get the AJAX call to work on production.

I think the error may be related to the custom routing I have set up in .htaccess (in my app’s root directory) and/or the 000-default-le-ssl.conf in the Apache config directory.

My .htaccess is:

<FilesMatch "^\.env$">
    Require all denied
</FilesMatch>

RewriteEngine On

# Exclude /rust path from being rewritten
RewriteCond %{REQUEST_URI} ^/rust
RewriteRule ^rust/(.*)$ http://localhost:8080/$1 [P,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

My 000-default-le-ssl.conf is:

<VirtualHost *:80>
    ServerName mywebsite.net
    ServerAlias www.mywebsite.net

    RewriteEngine on

    # Skip redirection for /rust path
    # RewriteCond %{REQUEST_URI} !^/rust

    # Redirect everything else to HTTPS
    RewriteCond %{SERVER_NAME} =mywebsite.net [OR]
    RewriteCond %{SERVER_NAME} =www.mywebsite.net
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    # Proxy configuration
    ProxyRequests Off
    ProxyPreserveHost On

    # ProxyPass for /rust/*
    #    RewriteCond %{REQUEST_URI} !^/rust
    #    ProxyPass /rust http://localhost:8080/
    #    ProxyPassReverse /rust http://localhost:8080/

    #    ProxyPass /rust/hello http://localhost:8080/hello
    #    ProxyPassReverse /rust/hello http://localhost:8080/hello

    DocumentRoot /var/www/html
    <Directory /var/www/html/>
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # Prevent access to .env files
    <FilesMatch "^\.env$">
        Require all denied
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

<VirtualHost *:443>
    ServerName mywebsite.net
    ServerAlias www.mywebsite.net

    DocumentRoot /var/www/html
    <Directory /var/www/html/>
        Options -Indexes
        AllowOverride All
        Require all granted
    </Directory>

    # SSL Configuration
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/www.mywebsite.net/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.mywebsite.net/privkey.pem

    # Redirect from HTTP to HTTPS
    RewriteEngine on
    RewriteCond %{HTTP_HOST} =mywebsite.net [OR]
    RewriteCond %{HTTP_HOST} =www.mywebsite.net
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [END,NE,R=permanent]

    # Prevent access to .env files
    <FilesMatch "^\.env$">
        Require all denied
    </FilesMatch>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I’ve tried so many permutations of permissions and routing, but I can’t figure out what I’m doing wrong.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video