unexpected end of file while adding httpauth - nginx

I'm trying to secure my phpMyAdmin with nginx http auth but, when I add auth instructions, nginx -t says:
nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/default.conf:22
nginx: configuration file /etc/nginx/nginx.conf test failed
server {
listen 80;
server_name x.x.x.x;
root /var/www/html/mypma;
index index.php index.html index.htm index.nginx-debian.html;
location = / {
return 444;
}
location = /pma {
auth_basic "Area riservata”;
auth_basic_user_file /etc/apache2/.htpasswd;
}
location ~ .php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
}

The second " was not a regular quote. Thanks to #tkausl's comment!

Related

Nginx error location directive is not allowed here when using include directive

I am using nginx with Docker, although I don't think the Docker part is relevant. I started with one docker/web/nginx.conf config file which looked like this:
upstream rails_app {
server app:3000;
}
server {
listen 80;
server_name _;
root $RAILS_ROOT/public;
index index.html;
access_log $RAILS_ROOT/log/nginx.access.log;
error_log $RAILS_ROOT/log/nginx.error.log;
location ~ /\. {
deny all;
}
location ~* ^.+\.(rb|log)$ {
deny all;
}
# Bunch more location directives...
}
I need to have a second server directive, which is mostly the same as the one above, but with one change. To avoid duplicating code, I want to use the include directive to include another shared configuration file (docker/web/shared.conf) that will contain the shared directives.
If I move just the root, index, access_log and error_log directives to the included file, it works well:
docker/web/nginx.conf
upstream rails_app {
server app:3000;
}
server {
listen 80;
server_name _;
include "conf.d/shared.conf";
location ~ /\. {
deny all;
}
location ~* ^.+\.(rb|log)$ {
deny all;
}
# Bunch more location directives...
}
docker/web/shared.conf
root $RAILS_ROOT/public;
index index.html;
access_log $RAILS_ROOT/log/nginx.access.log;
error_log $RAILS_ROOT/log/nginx.error.log;
However, as soon as I move the first location directive into the included file, the nginx server throws an error when starting:
docker/web/nginx.conf
upstream rails_app {
server app:3000;
}
server {
listen 80;
server_name _;
include "conf.d/shared.conf";
location ~* ^.+\.(rb|log)$ {
deny all;
}
# Bunch more location directives...
}
docker/web/shared.conf
root $RAILS_ROOT/public;
index index.html;
access_log $RAILS_ROOT/log/nginx.access.log;
error_log $RAILS_ROOT/log/nginx.error.log;
location ~ /\. {
deny all;
}
The error is:
2020/10/26 04:11:42 [emerg] 1#1: "location" directive is not allowed
here in /etc/nginx/conf.d/shared.conf:5 nginx: [emerg] "location"
directive is not allowed here in /etc/nginx/conf.d/shared.conf:5
Am I not allowed to have a location directive in the top-level of an included config file?

How to resolve Nginx error nginx: [emerg] unknown directive "server" in conf:1?

I changed the configuration of nginx, and then restarted nginx when the following error occurs.
[root#host ~]# nginx -s reload
nginx: [emerg] unknown directive "server" in /usr/local/openresty/nginx/conf/vhost/www.xxx.com.conf:1
Here is my configuration. How do I solve the problem?
server {
server_name www.xxx.com xxx.com;
listen 80;
listen 443;
ssl on;
ssl_certificate /data/https/domain.pem;
ssl_certificate_key /data/https/domain.key;
root /data/wwwroot/www.xxx.com;
index index.html index.htm index.php;
#rewrite
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ {
proxy_pass http://127.0.0.1:8080;
include proxy.conf;
}
location /nginx_status {
stub_status on;
access_log off;
#allow 127.0.0.1;
# deny all;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip: 9000;
#fastcgi_pass 127.0.0.1: 9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
access_log off;
}
Next, I used the-t test, and the result was like this.
[root#host ~]# nginx -t
nginx: [emerg] unknown directive "server" in /usr/local/openresty/nginx/conf/vhost/www.xxx.com.conf:1
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test failed
I just added the reverse proxy code and uploaded it to the server.
This part of the code can not be deleted, do not know how to solve the problem.
I found the problem, is windows notepad saved using BOM format, using vscode to re-select no bom format UTF-8.

Nginx merge two locations

I've split up most of my config files so it looks like this (simplified)
example.conf
server {
listen 80;
server_name example.com;
root /var/www/example.com/public_html;
#stuff......
#########
# Base WordPress configuration, setup php and everything
include /etc/nginx/snippets/wordpress.conf;
}
wordpress.conf
#stuff......
location / {
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php ;
}
#stuff......
So far so good. Now I want to secure only example.com with http-auth
So I add to the example.conf the following;
location / {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
But then I get the error;
nginx: [emerg] duplicate location "/" in /etc/nginx/snippets/wordpress.conf:53
nginx: configuration file /etc/nginx/nginx.conf test failed
What is the best way to enable http-auth on this domain only?
I could create an alternative wordpress.conf but that would defeat the idea of splitting up configs.

nginx: proxy_pass is not executed after eval block

I trying to get custom url from memcached server, assign it to variable using eval module and tell nginx to rewrite request.
My config file is included in http section of default nginx.conf and has the following code:
map $request_uri $instanceId {
"~/instance1" "instance1"; #"http://127.0.0.1:8088";
"~/instance2" "instance2"; #"http://127.0.0.1:8089";
}
log_format myVariable '{"evaluation_response" : "$proxyUrl", "for instance" : "$instanceId"}';
log_format redirectUrl '{"redirect" : "$proxyUrl/$relative_request_uri?$query_string"}';
server {
listen 8058;
server_name "memcahed.test.com";
location ~(?:\/instance\d)?\/(?<relative_request_uri>(?:\w*\W*)*){
eval_override_content_type text/plain;
eval_escalate on;
eval $proxyUrl {
if ($instanceId = '') {
return 502;
}
set $memcached_key $instanceId;
memcached_pass 127.0.0.1:11211;
}
#access_log /home/Vlad/myAccess.log myVariable;
proxy_pass "$proxyUrl/$relative_request_uri?$query_string";
#access_log /home/Vlad/myAccess.log redirectUrl;
}
}
server {
listen 8088;
listen [::]:8088;
server_name "i1.test.com";
root /var/www/test.com/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}
server {
listen 8089;
listen [::]:8089;
server_name "i2.test.com";
root /var/www/test2.com/html;
index index.html index.htm;
location / {
try_files $uri /index.html;
}
}
The servers are fake to serve static files - I do not have real ones. When I enable logging - I see correct output:
{"evaluation_response" : "http://127.0.0.1:8088", "for instance" : "instance1"}
{"redirect" : "http://127.0.0.1:8088/?-"}
{"evaluation_response" : "http://127.0.0.1:8089", "for instance" : "instance2"}
{"redirect" : "http://127.0.0.1:8089/?-"}
But page never loads (Chrome message):
The ::IP:: page isn’t working ::IP:: didn’t send any data.
ERR_EMPTY_RESPONSE
The error.log does not contain any useful information (output level - debug), but if you need it - I can provide a copy for one request.
What is wrong with my configuration?

Nginx invalid URL prefix

I have a really basic nginx setup which is failing for some reason;
server {
listen 80;
server_name librestock.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/david/StockSearch/stocksearch;
}
location / {
include proxy_params;
proxy_pass unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
}
}
according to everything I've read I'm setting the server name correctly.
when I replace librestock.com with the ip of the server it works.
error:
$ nginx -t
nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/stocksearch:12
nginx: configuration file /etc/nginx/nginx.conf test failed
You need the http:// prefix on your unix: path, as in:
proxy_pass http://unix:/home/david/StockSearch/stocksearch/stocksearch.sock;
See
http://nginx.org/en/docs/http/ngx_http_proxy_module.html

Resources