I have found answers here on redirecting php and error codes, but not from one page to another. An aspect of my website is a FTP uploader and my extension is mywebsite/ss/xyz.file. Currently, I have an index.html setup in /ss so people may not see my screenshots, however I would rather just give them a 404, without making all 404s go to /ss, or automatically redirect them to my homepage.
My current serverblock config:
server {
listen 80;
listen [::]:80;
root /var/www/xxx.xxx/html;
index index.html phpinfo.php index.htm index.nginx-debian.html;
server_name mangoman.me www.xxx.xxx
location \ {
proxy_pass http://localhost:8080;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
error_page 404 /custom_404.html;
location = /custom_404.html {
root /usr/share/nginx/html;
internal;
}
}
I actually figured out my own answer.
location /ss {
return 404;
}
Related
I would like to serve some of my website static assets like .txt, .csv, images and other document types as non https. But my current configuration always redirects to https.
server {
listen 80;
listen [::]:80;
root /var/www/public;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
return 301 https://$host$request_uri;
}
location /static/ {
root /var/www/public/static;
}
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
include snippets/ssl-example.com.conf;
include snippets/ssl-params.conf;
root /var/www/public;
index index.php index.html index.htm;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
}
}
I have been reading similar questions here in SO:
Nginx: Redirect all to https except one laravel url = too many redirects
nginx: redirect everything from http to https, except one url-pattern
But nothing works for me and worst returns too many redirect.
I am trying to run a react app with Node.js backend on the Nginx server.
Here's my server block in the nginx.conf file:
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/folder-name/frontend/build;
index index.html index.htm;
location / {
proxy_pass http://localhost:5000;
}
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
The build folder contains the compiled react js code(using npm run build)
Node.js backend is running on port 5000 using pm2/forever.
After restarting the Nginx Server, the react UI appears on the server IP but the UI is distorted.
Also, I am not able to access my backend APIs on MyIP/api/endpoint.
What am i doing wrong.? This nginx configuration was built from SO and other online resources so there's a huge probabilty that it could be wrong. Please help!
Thanks in advance!
The issue is you are setting the API proxy for the root (/). The correct one should look like this:
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/folder-name/frontend/build;
index index.html index.htm;
location /api {
proxy_pass http://localhost:5000;
}
location / {
try_files $uri $uri/ =404;
}
}
If you don't have /api path in your Node.js, you will need a rewrite rule for it like this:
location /api {
rewrite ^/api/?(.*) /$1 break;
proxy_pass http://localhost:5000;
proxy_redirect off;
proxy_set_header Host $host;
}
I had experience that.Please check my image file
This configuration is running successfully on aws.
Your mistakes is proxy area. Please change like that.
location /api/ {
proxy_pass http://127.0.0.1:5000/api/
}
If you want, I can HELP you.
Yes, you can host both API and static files (build files of your front-end) on the same domain or host. Below, you can find a server block for a sample API hosted on port 3000 and static HTML files at a root location being served on port 80.
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
error_page 404 index.html;
}
location /api/ {
proxy_pass http://localhost:3000/;
}
}
You can access the front-end at http://localhost/<blah...> and the API at http://localhost/api/<blah...> (please note how /api is handled in the URL here and the server block above). Replace localhost with your domain name.
What am i doing wrong.?
One issue is with your proxy_pass directive. You are missing trailing slash /
...
location / {
proxy_pass http://localhost:5000/;
}
...
First, try this and share your result.
I am trying to rewrite a URL pattern to sub-folder.
location ~^ /test/article/(.*) {
root /var/www/example.com/test;
index index.html;
try_files $uri $uri/ /test/index.html;
}
When a user requests http://www.example.com/test/article/article-name,
I need to rewrite the URL to http://www.example.com/test/, but URL should stay the same, it should display as http://www.example.com/test/article/article-name in the browser.
"article-name" could be any thing with dashes. But "article" is the hard coded path, not a real folder.
My server is currently hosting Wordress in root directory of Nginx server which is /var/www/example.com/ . I tried different things with Nginx config file and not working yet. Currently, when I typed in http://www.example.com/test/article/article-name , Wordpress will show 404 error.
Any help would be appreciated.
Thank you.
Updates:::::::
Here is my latest config file under sites-available folder. It's showing Wordpress's home page when I tried the URL.
server {
listen 8080 default_server;
listen [::]:8080 default_server;
port_in_redirect off;
set_real_ip_from 127.0.0.1/32;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
root /var/www/example.com;
index index.php index.html;
server_name example.com www.example.com;
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
#
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
include /etc/nginx/fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
Another update:
I am now testing with following config and it doesn't show Wordpress's 404 anymore. But it actually redirects to /test folder. I need to keep the url with "/test/article/article-name" in it.
rewrite ^/test/article/(.*) /test/;
location /test/ {
index index.html;
}
rewrite ^/test/article/(.*) /test;
location /test {
root /var/www/example.com/;
}
# create file `test` in `/var/www/example.com/`
Choice II
rewrite ^/test/article/(.*) /test/;
location /test/ {
root /var/www/example.com/test/;
index index.html;
}
# create directory `test` in `/var/www/example.com/`
# and create file `index.html` in `/var/www/example.com/test/`
I have a nodejs app served with nginx for static files. On reading the sites_available of nginx, I saw root was assigned a static file serving folder (public folder to be exact). What does this mean?
My configuration looks like
server {
server_name mysite.mydomain.com;
root /var/www/mysite/public/;
index index.html index.htm;
access_log /var/log/nginx/mysite.access.log;
error_log /var/log/nginx/mysite.error.log;
underscores_in_headers on;
recursive_error_pages on;
error_page 503 #maintenance;
if (-f $document_root/system/maintenance.html) {
return 503;
}
location #maintenance {
error_page 405 = /system/maintenance.html;
rewrite ^(.*)$ /system/maintenance.html break;
}
location ~ ^/(js|css|images|media|system)/ {
autoindex off;
add_header Cache-Control public;
expires 4w;
}
location / {
try_files $uri $uri/index.html #proxy;
}
location #proxy {
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:7485;
}
}
Can any one explain this to me? I searched through the nginx documentation and didn't find any answer to my queries.
The root directive sets the base path for the requests. In other words, it is used to translate http request paths into file system paths.
For example, with your root set as /var/www/ under the server directive, if a file http://www.example.com/image.jpg is requested, it's path will be translated to /var/www/image.jpg
More details can be found here: http://nginx.org/en/docs/http/ngx_http_core_module.html#root
My client moved to new server which nginx & percona server. Problem is the forward slash is being removed from end of the url. as you can see on the picture http://imgur.com/8OAUQZb
therefor the design files such as js, css files are not loading or something it gives magento 404 not found page. it happens randomly.
on the database web/unsecure/base_url and web/secure/base_url are set with forward slash http://78.137.115.47.srvlist.ukfast.net/
I'm assuming something wrong with nginx file. The rewrite rules might be wrong. I have tried every possible way that I found on this website and on google but nothing work. It might be something else. Could you please help?
This is nginx conf file for the domain
# Uncomment the server definition below should you wish to
# redirect from punkyfish.com to www.punkyfish.com
#server {
# listen 192.168.92.247;
# server_name punkyfish.com;
# #rewrite / $scheme://www.$host$request_uri permanent;
#}
#
# Change this backend name (and the socket pointer)
# as additional virtual hosts are added. This should
# point to the spawn-fcgi wrapper running as the
# appropriate user.
#
upstream punkyfishcombackend {
server unix:/var/run/php-fcgi-punkyfishcom.sock;
}
server {
listen 192.168.92.247:80;
server_name punkyfish.com;
root /var/www/vhosts/punkyfish.com/htdocs;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
location /. {
return 404;
}
location #handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass punkyfishcombackend;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param MAGE_RUN_CODE default;
# fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
This was due to nginx was caching everything. regardless of my changes on db and files wasn't reflecting. I cleaned the cache from ngnix cache folder and problem solved.
Thanks.