Nginx configuration for Symfony on a sub uri - nginx

I am trying to host two applications under the same domain. One nodejs (nextjs) project and a Symfony application. I was planning to use "/be" sub url for the Symfony application. I was able to setup nginx to route to the respective applications. However I am getting 404 because I cannot replace the "/be" prefix from the request uri while trying to pass the request to the PHP-FPM. I tried replacing the REQUEST_URI fastcgi parameter but same problem persists. Here's my location configurations:
location ~ ^/be/.*$ { # for static files
root /home/ubuntu/be-prereg/public;
# remove /be/ from request url
rewrite ^/be/(.*)$ /$1 break;
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
root /home/ubuntu/be-prereg/public;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param REQUEST_URI '/api/test';
internal;
}
#nodejs
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

Related

Yii v1.1 redirects to 404 nginx

My Yii 1.1 app redirects to site.com/login and gets error 404. Yii app hasn't any problems, it's an old app which I want to install on my server. The problem has to be with nginx configuration with mod_rewrite because pretty urls are not processed as they should.
Here i my default.conf:
server {
include snippets/phpmyadmin.conf;
listen 80 default_server;
charset UTF-8;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /home/fornex/tmp_7offers/public/;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php;
server_name localhost;
client_max_body_size 30m;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 60;
fastcgi_send_timeout 60;
location /public {
root /home/fornex/tmp_7offers/public/;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
error_log /var/log/nginx/error-01.log;
index index.php index.html index.htm;
# proxy_pass http://localhost:8080;
# proxy_http_version 1.1;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection 'upgrade';
# proxy_set_header Host $host;
# proxy_cache_bypass $http_upgrade;
if (!-e $request_filename){
rewrite ^/(.*) /index.php?r=$1 last;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include snippets/fastcgi-php.conf;
include fastcgi_params;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
if ($request_uri ~ ^/(login)) {
error_page 404 =301 #redirect;
}
# With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
How can I enable mod_rewrite so pretty urls will processed as they should?

PhpMyAdmin wrong display in Nginx using reverse proxy

I am running a NodeJS application in Nginx with a reverse proxy to port 3000
PhpMyAdmin is configured and apparently runs in /phpmyadmin
/etc/nginx/sites-available/default is configured like this:
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost mywebsite.com www.mywebsite.com;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /phpmyadmin {
root /var/www/html/phpmyadmin/;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
}
I managed to make all work as I wanted, but PhpMyAdmin has a bad display:
Before setting the reverse proxy in that file, PhpMyAdmin ran perfect. I assume there is something I missed in default file. Any ideas? Thanks
To Ivan Shatsky's Response: #IvanShatsky
I tried substituting with your code, but it downloads a file (it does not read .php files) - So, I added few more lines:
location ^~ /phpmyadmin {
index index.php;
try_files $uri $uri/ /phpmyadmin/index.php$is_args$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
The result is the same before.
When I check the resources of the network, this is what happens:
I have performed chmod 777 -R /var/www/html/phpmyadmin but no changes. Would I need to have https? - I am currently trying on http.
I wonder how it is running at all. Assuming your phpMyAdmin located in the /var/www/html/phpmyadmin directory, try this:
location /phpmyadmin {
index index.php;
try_files $uri $uri/ /phpmyadmin/index.php$is_args$args;
}

Nginx: Nuxtjs app on main domain + WordPress in a subdirectory

I have a nuxtjs app https://damjanun.com/ running on main domain with nginx proxy setup. Now we need to install wordpress inside a sub-directory i.e: https://damjanun.com/blog . I can't get it done.
Found the solution. Here is my nginx config file for damjanun.com. The blog also works fine https://damjanun.com/blog
location /blog {
try_files $uri $uri/ /blog/index.php?q=$1;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}

nginx => Serve two websites from one server block with sub-domain

I'd like to serve two applications from the same server through nginx. I'd like these applications to be available through a single domain name with sub-uris.
e.g.
www.example.com => should serve normal example site.
www.example.com/blog => blog.example.com (wordpress) site which is at different directory
Here, anything user requests from /blog/... should be served from blog site, but url should be www.example.com/blog/...
I have tried this.
server {
root /web/servers/example/public;
...
...
location /blog {
proxy_pass http://blog.example.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Other try was,
location /blog {
alias /web/servers/blog/public;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
#allow 127.0.0.1;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_index index.php;
}
}
None of the options are working for /blog. Main site is working fine.
Could anybody please help me to solve the issue?

Nginx load balancer websocket issue

I'm new in NGINX and WebSocket systems but as per my project requirements I need to check some complex things to finish.
I'm trying to create one example using NGINX, which handles my WebSocket (Port: 1234) and HTTP Requests (Port: 80) using same Url (load balancer url).
I'm using three NGINX server, one as Load Balancer (10.0.0.163) and other two as my application server where I have installed my real APIs, 10.0.0.152 and 10.0.0.154 respectively. Right now, I have configured WebSocket on my application servers.
As per above configuration, my all requests will pass over 10.0.0.163 (load balancer) and it's proxy setting will pass the request (HTTP/WebSocket) to my application server (10.0.0.152/154).
Note : Each application server contain separate Nginx, php, websocket
Here is default (location : /etc/nginx/sites-available/) file for 10.0.0.154 server, which handles WebSocket and HTTP requests on same domain.
server{
listen 80;
charset UTF-8;
root /var/www;
index index.html index.htm index.php
server_name localhost 10.0.0.154 ;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
try_files $uri $uri/ #proxy; autoindex on;
}
location #proxy{
proxy_pass http://wb1;
}
location =/ {
proxy_pass http://wb;
proxy_http_version 1.1;
proxy_buffers 8 16k;
proxy_buffer_size 32k;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer $http_referer;
proxy_redirect off;
}
}
Following is default file (location : /etc/nginx/sites-available/) for load balancer at 10.0.0.163.
upstream wb{
server 10.0.0.154;
server 10.0.0.152;
}
server{
listen 80;
charset UTF-8;
root /var/www;
index index.html index.htm index.php
server_name 10.0.0.163 ;
location / {
try_files $uri $uri/ #proxy; autoindex on;
}
location #proxy{
proxy_pass http://wb;
}
location =/ {
proxy_pass http://wb;
proxy_http_version 1.1;
proxy_buffers 8 16k;
proxy_buffer_size 32k;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer $http_referer;
proxy_redirect off;
}
}
I found that, load balancer is working properly for HTTP requests but it's unable to proceed my WebSocket requests to my application server.
I don't know what I'm missing here .. If you guys can help me out would be great appriciate
I seen your configuration looks proper. I think you should check your load balancer & your application server configuration or versions. It maybe problem of incompatibility.

Resources