Nginx url rewrite error : [emerg] unexpected "}" - linux

I'm trying to rewrite urls in nginx, blow is my sample code :
server {
listen 80;
server_name example.com;
root /full/server/path/to/your/cms;
index index.php;
location / {
try_files $uri $uri/ /phphandler
}
location /phphandler {
internal;
# nested location to filter out static items not found
location ~ .php$ {
rewrite ^/([^/]*)(.*) /$1 break;
fastcgi_pass 127.0.0.1:8080;
...
}
}
}
I'm using DirectAdmin and when I add this block of code manually from linux or from DirectAdmin, I face the same error :
nginx: [emerg] unexpected "}" in
/usr/local/directadmin/data/users/admin/nginx.conf:178 nginx:
configuration file /etc/nginx/nginx.conf test failed
Notice : I'm copy pasting this peace of code from Here
And I'm in doubt that the problem is where the 3 dots( ... ) are ( after fastcgi_pass 127.0.0.1:8080; ) I think there's some thing I should add here that I don't know?!
Is there anything I'm missing in this block of code?
Or there's another problem that I face this error?
Thanks in advance from anyone who helps me solve this problem :)

The problem is that you miss ; at the end of the try_files.
But also this doesn't help much too.
As you mentioned you're using DirectAdmin I recommend you use Nginx - Apache Reverse proxy.
First of all make sure you've got custombuild 2
cd /usr/local/directadmin/custombuild
./build version
You should see an output similar to the following:
2.0.0-RC7 (rev: 863)
Also make sure to have version of directadmin no less than 1.45.2 otherwise the things won't work.
/usr/local/directadmin/directadmin v
Run this code to build nginx + apache with custombuild:
cd /usr/local/directadmin/custombuild
./build update
./build update_da
./build set webserver nginx_apache
./build nginx_apache
./build rewrite_confs

Related

nginx dynamic change root of subdomain to folder on same level

I have this hierarchy on my server:
/var/www/domain.tld/web
/sub
The root in the /etc/nginx/sites-available/domain.tld is set like this:
server {
root /var/www/domain.tld/web;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}
How can I make the nginx to dynamically forward to specific folder so if I make folder in /sub for example /var/www/domain.tld/sub/subdomain so if I make request http://subdomain.domain.tld it will forward to this folder in folder sub?
So again something.domain.tld would be going to /var/www/domain.tld/sub/something but domain.tld would be going to /var/www/domain.tld/web
Thank you!
First off, welcome to StackOverflow! :)
There are probably a few ways of achieving this, but I would try a simple map to set a $my_subdir variable from the $host, which you can then use in your root. The $host variable is set automatically from the incoming request.
In this example, we're using a regular expression to check if the $host matches subdomain.domain.tld. If it does, capture the subdomain part - the bit in brackets - and use that to make the value "sub/subdomain" ($1 means use the first thing captured from the regular expression). If it doesn't match, it defaults to "web".
That way,
when the request comes in as something like http://domain.tld/foo.html, the response served is /var/www/domain.tld/web/foo.html
if the request comes in as something like http://bar.domain.tld/foo.html, the response served is /var/www/domain.tld/sub/bar/foo.html
map $host $my_subdir {
~^(?:www\.)?((?!www\.)[^.]+)\.domain\.tld$ sub/$1;
default web;
}
server {
root /var/www/domain.tld/$my_subdir;
index index.html index.php index.htm;
...
location / {
try_files $uri $uri/ =404;
}

How can I configure my Nginx file to correctly display my React app?

I've got my build folder on my server and I assume I've correctly configured nginx to be able to see it (I copied the config from Deploy Create-React-App on Nginx), but I still get a 404 page when I try to navigate to it. Is my nginx file just not configured right? I can't figure out what could be causing this problem.
I've tried following Deploy Create-React-App on Nginx exactly, changing things to match my own server name etc. when appropriate.
This is the section I've added to my config file:
server {
listen 80;
server_name xxx.net www.xxx.net;
root /var/www/xxx.net/html/build;
index index.html;
location /news {
try_files $uri /index.html;
}
}

Htaccess and nginx converting

I'm converting this htaccess file htaccess file
to nginx using
http://winginx.com/ru/htaccess
but don't understand where i should paste result. I have created
include file
include /etc/nginx/myfile
and pasted file there but when i'm reloading (restarting) Nginx it Fails.
Could you help me?
I suspect these apache .htaccess converters do not fully utilize the unique features of nginx. I would recommend trying something like this:
server {
listen 80 default_server;
server_name _;
root /var/www/example.com;
index index.php;
# if the file or directory doesn't exist, serve /index.php
try_files $uri $uri/ /index.php;
# if the request is exactly /sitemax.xml, serve sitemap_xml.php
location = /sitemax.xml {
try_files /modules/sitemap/sitemap_xml.php =404;
}
# hide regex location in a prefix location to avoid confusion
# introduced by multiple regex locations
location /pages-print {
location ~ ^/pages-print(\d+) {
try_files /modules/pages/print.php?page=$1 =404;
}
}
}
I have found solution. I tried paste result from the convertor to nginx.conf and it was mistake. I, instead created file in /etc/nginx/ and named it htac.rules (i think it doesn't matter of how you'll name it).
Then i opened file etc/nginx/sites-enabled/default and in location / inserted include /etc/nginx/htac.rules
so.
in htac.rules i inserted converting result without location / {...}
and, yeah. It solved my problem.
Thanks everyone for helping me.

Nginx redirect all media (.mp3 , image , pdf) to MaxCDN origin poll

I want to redirect all my media files to maxcdn origin poll, this might be duplicate question
but couldn't find the way i was looking for.
example:
If visitor request something like : http://domain.com/monday/day1.mp3 it needs to redirect to http://xxx.netdna.com/monday/day1.mp3.
Question: --- No To This :/
Will nginx allow .htaccess to do this job?
Or
Do I need to setup server config? How
Here is my config, which is every simple.
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/domain/public_html;
index index.html index.php index.htm;
# Make site accessible from http://localhost/
server_name domain.com;
return 301 http://XXX.YYYY.netdna-cdn.com$request_uri;
The page isn't redirecting properly
Please Help!
Will nginx allow .htaccess to do this job?
No. There is no .htaccess analogue for nginx.
This will redirect all request to folder /podcast/ to xxx.netdna.com.
location /podcast/ {
return 301 http://xxx.netdna.com$request_uri;
}
I have found solution for my problem after a huge dig search on google, but the answer was so simple, didn't realized till it turned on working perfectly for my problem... :)
All I need to add was, following code right after location /
Here is what i added and worked like charm.
try_files $uri $uri/ /index.php;

nginx: [emerg] unknown directive " " in /etc/nginx/sites-enabled/example.com:3

I have followed this website http://raspberrypihelp.net/tutorials/24-raspberry-pi-webserver to setup the HTTP server nginx on my Raspberry Pi and try to setup a site call example.com. But when I run sudo service nginx restart, it said
Restarting nginx: nginx: [emerg] unknown directive " " in /etc/nginx/sites-enabled/example.com:3
Here is the code in example.com.
server {
server_name example.com 192.168.1.88;
access_log /srv/www/example.com/logs/access.log;
error_log /srv/www/example.com/logs/error.log;
root /srv/www/example.com/public/;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public$fastcgi_script_name;
}
location /phpmyadmin {
root /usr/share/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /usr/share/;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /usr/share/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
}
I am just following the steps but it can't run successfully.
I had the same problem which was that I copy/pasted the config code from the web and some dirty EOL(end of line) characters where there.
The editor didn't show them, but nginx treated them like a directive.
Just deleted every EOL and added again.
It sounds like you did some copy and paste work here. It's not uncommon to snag some extra characters that are invisible at the end of line (EOL). Try this:
Run your text through this tool:
http://www.textfixer.com/tools/remove-line-breaks.php
then fix any breaks that may have been removed and will be affected by the comments.
This worked for me. Hope it works for you.
It looks like the nginx binary was compiled with --without-http_fastcgi_module option.This is not default. Try donwloading or compiling a different binary.
Try running
nginx -V
(with uppercase V) to see what options were used to compile the nginx.
I edited some text in the mid of the conf file and nginx started showing this error at the starting of the file itself. I copied the contents of the file, created a new file, pasted the contents there and nginx stopped showing this error.
I faced similar issue with error message as "unknown directive 'index.html'" when running 'sudo nginx -t'. After correcting the HTML syntax errors in index.html, the issue was resolved.
Even if you miss a semicolon you will encounter the same error.
// Missed semicolon
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock
// With semicolon
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
In my case, I have store configuration file nginx.conf in the github. I have done wget to raw version of code, thus resulted this error.
Later, I have cloned my repository and used the nginx.conf file from clone and issue got resolved.
Really didn't got how that happened,
I've "docker run " my nginx 1.14 (default apt for debian:bullseye) to extract it's default nginx.conf, with intention to update it and than use it into my Dockerfile.
Anyhow, after having read comments in this thread, found this that the file is
"UTF-16LE" ... I'm not really expert, but is not "UTF-8".
solved as:
Issue seen from inside the container:
me#docker-nginx $ head nginx.conf
��
user www-data:qgis;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
error_log /dev/stdout warn;
events {
worker_connections 768;
me#docker-nginx $ dos2unix nginx.conf
dos2unix: converting UTF-16LE file nginx.conf to UTF-8 Unix format...
Solved also on working dir:
IntelliJ IDEA: select "convert" after "UTF-8"
I had the same problem when I looked inside my config file there was a syntax error so this might be the problem check this path
nano /etc/nginx/sites-available/example.com

Resources