Overpass API with nginx - nginx

In:
https://wiki.openstreetmap.org/wiki/Overpass_API/Installation
I can read:
Setting up the Web API only for apache. Is it possible for Nginx?
I am trying it but I always find 405 Not Allowed if I ask remotely
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8000;
location /api/ {
alias /mnt/data/openstreetmap/osm-3s_v0.7.4/cgi-bin/;
}
#
location /cgi-bin/ {
gzip off;
root /mnt/data/openstreetmap/osm-3s_v0.7.4/;
fastcgi_read_timeout 900;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /opt/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location / {
root /mnt/data/openstreetmap/osm-3s_v0.7.4/html;
index index.html index.htm;
}
}
}
In localhost:
wget --output-document=test.xml http://localhost:8000/api/interpreter?data=%3Cprint%20mode=%22body%22/%3E
--2016-08-14 18:07:38-- http://localhost:8000/api/interpreter?data=%3Cprint%20mode=%22body%22/%3E
Petición HTTP enviada, esperando respuesta... 200 OK
Longitud: 1983984 (1,9M) [application/octet-stream]
Grabando a: “test.xml”
test.xml 100%[======================================================================>] 1,89M --.-KB/s in 0,004s
2016-08-14 18:07:38 (488 MB/s) - “test.xml” guardado [1983984/19839
In browser (remote client):
405 Not Allowed
No problem with access to index.html

write this
rewrite ^/api/(.+)$ /cgi-bin/$1 last;
instead of
location /api/ {
alias /mnt/data/openstreetmap/osm-3s_v0.7.4/cgi-bin/;
}

Related

Redirecting nginx causes 'too many redirects' error

I need to redirect both www to non www and HTTP to HTTPS with nginx. I can get it to redirect but then I get a `too many redirects' error.
I'm using the Azure AppService version of WordPress. This version uses the wordpress-alpine-php docker image, running nginx version 1.20.2.
The nginx.conf file includes:
/etc/nginx/conf.d/*.conf
/etc/nginx/modules-enabled/*.conf
I don't see a modules-enabled directory.
For the HTTP to https redirect, I added the following server directive to default.conf:
server {
listen 80;
server_name ---.com www.---.com;
return 301 https://---.com$request_uri;
}
After this, I get the "too many redirects" error.
I noticed the following server block also listens on port 80, so I changed it to 443. I still get the "too many redirects".
Below are my conf files. The only change I made was adding the server directive above, and changing the port to 443 in the original server directive.
How do I get these redirects to work?
Could there be other files involved?
/etc/nginx/nginx.conf
user nginx;
worker_processes auto;
# send nginx error logs to stderr
error_log /dev/stderr error;
pid /var/run/nginx.pid;
load_module modules/ngx_http_brotli_static_module.so;
load_module modules/ngx_http_brotli_filter_module.so;
events {
worker_connections 10000;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log off;
sendfile on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/modules-enabled/*.conf;
}
/etc/nginx/conf.d/default.conf
upstream php {
server unix:/var/run/php/php-fpm.sock;
#server 127.0.0.1:9000;
}
server {
listen 80;
server_name ---.com www.---.com;
return 301 https://---.com$request_uri;
}
server {
listen 443;
## Your website name goes here.
server_name _;
if ($http_x_forwarded_proto = "http") {
return 301 https://---.com$request_uri;
}
## Your only path reference.
root /home/site/wwwroot;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Add locations of phpmyadmin here.
location /phpmyadmin {
root /home/;
index index.php index.html index.htm;
location ~ ^/phpmyadmin/(.+\.php)$ {
try_files $uri =404;
root /home/;
fastcgi_pass unix:/var/run/php/php-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 /home/;
}
}
location /phpMyAdmin {
rewrite ^/* /phpmyadmin last;
}
# Disable sendfile as per https://docs.vagrantup.com/v2/synced-folders/virtualbox.html
sendfile off;
set $skip_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $skip_cache 1;
}
if ($query_string != "") {
set $skip_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# Don't cache WooCommerce URLs
# Cart widgets are still a problem: https://github.com/emcniece/docker-wordpress/issues/3
if ($request_uri ~* "/(cart|checkout|my-account)/*$") {
set $skip_cache 1;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~* \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
fastcgi_read_timeout 300;
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache off;
fastcgi_cache_valid 60m;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
You need to add ssl flag to your listen directive if you want to use HTTPS. And specify your SSL certificate - something like this:
server {
listen 443 ssl http2;
server_name ---.com;
ssl_session_cache shared:SSL:4m; # measured in megabytes, not minutes
ssl_buffer_size 4k; # reduced from the default 16k to minimize TTFB
ssl_session_timeout 30m;
ssl_session_tickets on; # Requires nginx >= 1.5.9 (SSL labs testing leads to SSL: error:14094085:SSL routines:SSL3_READ_BYTES:ccs received early)
ssl_dhparam /etc/ssl/dhparam.pem; # Generate with "openssl dhparam -out dhparam.pem 4096"
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.3 TLSv1.2;
ssl_stapling on;
ssl_stapling_verify on;
resolver 1.1.1.1 valid=300s ipv6=off;
resolver_timeout 4s;
ssl_certificate /etc/ssl/fullchain.pem;
ssl_certificate_key /etc/ssl/key.pem;
.......
}

How to redirect nginx request to different path

Here is my nginx.conf which serves any request coming to example.com with /srv/phabricator/phabricator/webroot/index.php. I want to change the functionality such that if a request comes in to example.com/test then /home/phragile/public/index.php is served.
daemon off;
error_log stderr info;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 4096;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
client_max_body_size 200M;
client_body_buffer_size 200M;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket_pool {
ip_hash;
server 127.0.0.1:22280;
}
server {
listen *:80;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /srv/phabricator/phabricator/webroot;
try_files $uri $uri/ /index.php;
location /.well-known/ {
root /srv/letsencrypt-webroot;
}
location / {
index index.php;
if ( !-f $request_filename )
{
rewrite ^/(.*)$ /index.php?__path__=/$1 last;
break;
}
}
location /index.php {
include /app/fastcgi.conf;
fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin";
fastcgi_pass 127.0.0.1:9000;
}
location = /ws/ {
proxy_pass http://websocket_pool;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 999999999;
}
}
}
I have tried the following but it does not work:
location /test {
root /home/phragile/public;
}
Can someone show me what needs to be added in the .conf file?
You will need to use alias rather than root as you are attempting to map /test to /home/phragile/public and the latter does not end with the former. See this document for more. You will also need to execute PHP within that location (see your location /index.php block).
You have a very specific configuration that is designed to execute just one PHP file. A general solution for /test might look like:
location ^~ /test {
alias /home/phragile/public;
if (!-e $request_filename) { rewrite ^ /test/index.php last; }
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include /app/fastcgi.conf;
fastcgi_param PATH "/usr/local/bin:/usr/bin:/sbin:/usr/sbin:/bin";
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
I have pasted your existing FastCGI directives (which I presume is working for you) and added the required value for SCRIPT_FILENAME (assuming you are using php_fpm or similar).
Of course, if there is no static content under /test you can simplify considerably.

Nginx development machine setup

I am trying to setup my windows development machine with nginx and I would like to be able to run multiple sites. What I tried to do is assign different ip addresses for each domain in nginx.conf and hosts files as below:
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
gzip on;
server_tokens off;
add_header X-Frame-Options Deny;
expires 365d;
include mime.types;
server {
listen 172.76.0.10:80;
server_name mywebsite.local;
root /Websites/mywebsite_dev;
location / {
index index.php;
}
location ~ \.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
server {
listen 172.76.0.11:80;
server_name mywebsite-two.local;
root /Websites/mywebsite-two_dev;
location / {
index index.php;
}
location ~ \.php {
include fastcgi.conf;
fastcgi_pass 127.0.0.1:9000;
}
}
}
hosts
172.76.0.10 mywebsite.local
172.76.0.11 mywebsite-two.local
But when I try to run the nginx service I get this error:
nginx: [emerg] bind() to 172.76.0.10:80 failed (10049: FormatMessage() error:(15100))
Anyone has an idea on what I'm doing wrong?

nginx location index directive not working

I'm new to nginx and I just can't determine why my nginx config doesn't work as expected. All I want to do is to make nginx prioritize index.html over index.php for every web root (/) request.
This is my nginx config:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
multi_accept on;
}
http {
##
# Basic Settings
##
server {
location / {
index index.html index.php;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 15;
keepalive_requests 100000;
types_hash_max_size 2048;
client_body_in_file_only clean;
client_body_buffer_size 32K;
client_max_body_size 300M;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
----------------- cut ---------------
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Where's my error? What's the correct way to write this nginx config?
If you explicitly request /index.html, is it served? If not, you might want to add an explicit root /path/to/root; to your server {} block. Also verify that index.html has the correct permissions.
This will help with troubleshooting: It will force a 404 if the root index.html is not found. If that happens, at least you can check the logs to see were it was looking:
location = / {
index index.html;
}
Also, be sure to do nginx -s reload when changing the config.
Convention:
You should keep location and server declarations in virtual host files (/etc/nginx/conf.d/*.conf; and /etc/nginx/sites-enabled/*;, as you can see from the nginx conf). Files in /etc/nginx/conf.d/*.conf; are typically symlinked to files in /etc/nginx/sites-enabled/*; in order to become "enabled"
Some things to try
See my blog post here which has a setup similar to yours.
Try moving your index index.html index.html index.php files directive outside of a location {} block

How to create custom error 502 nginx in core of nginx (not using redirect to error page)?

Sometimes I get an issue with error 502 when httpd service is down.
But only in 1 minute the website come back.
I need to custom the 502 message to ask user to wait for 1 minute then refresh page, or embed JavaScript or meta refresh tag to auto refresh page after 1 minute.
Page's URL must be the same to make refresh effect
Notice that I know about custom error page redirect eg location = /502.html, but that type of custom error page will redirect user to other page, if they will refresh page they will got error page again.
Any idea will be very helpful.
EDIT UPDATE for more detail 10/06/2012.
My nginx config:
user nobody;
# no need for more workers in the proxy mode
worker_processes 24;
error_log /var/log/nginx/error.log crit;
#worker_rlimit_nofile 20480;
events {
worker_connections 109024; # increase for busier servers
use epoll; # you should use epoll here for Linux kernels 2.6.x
}
http {
server_name_in_redirect off;
server_names_hash_max_size 2048;
server_names_hash_bucket_size 256;
include mime.types;
default_type application/octet-stream;
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 20;
ignore_invalid_headers on;
client_header_timeout 50m;
client_body_timeout 50m;
send_timeout 20m;
reset_timedout_connection on;
connection_pool_size 2048;
client_header_buffer_size 256k;
large_client_header_buffers 4 256k;
client_max_body_size 20M;
client_body_buffer_size 300k;
request_pool_size 32k;
output_buffers 14 32k;
postpone_output 1460;
proxy_temp_path /tmp/nginx_proxy/;
proxy_cache_path /dev/shm/nginx levels=1:2 keys_zone=wwwcache:45m inactive=5m max_size=1000m;
client_body_in_file_only off;
access_log off;
open_log_file_cache off;
#log_format bytes_log "$msec $bytes_sent .";
include "/etc/nginx/vhosts/*";
}
and vhost config:
server {
# error_log /var/log/nginx/vhost-error_log warn;
listen 123.30.137.66:80;
server_name xaluan.net mtvvui.com www.daiduong.com.au www.xaluan.net xaluan.com www.xaluan.com www.daiduongrestaurant.net veryzoo.com www.mtvvui.com www.xaluan.org www.veryzoo.com daiduongrestaurant.net xaluan.org daiduong.com.au;
# access_log /usr/local/apache/domlogs/xaluan.net combined;
root /home/xaluano/public_html;
location / {
if ($http_cache_control ~ "max-age=0") {
set $bypass 1;
}
location
~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$
{
#root /home/xaluano/public_html;
#proxy_cache wwwcache;
#proxy_cache_valid 200 15m;
#proxy_cache_bypass $bypass;
expires 1d;
#try_files $uri #backend;
proxy_pass http://123.30.137.66:8081;
}
error_page 405 = #backend;
add_header X-Cache "HIT from Backend";
#proxy_set_header Server "Caching-Proxy";
#add_header X-Cache-Vinahost "HIT from Backend";
proxy_pass http://123.30.137.66:8081;
include proxy.inc;
}
location #backend {
internal;
proxy_pass http://123.30.137.66:8081;
include proxy.inc;
}
location ~ .*\.(php|jsp|cgi|pl|py)?$ {
#proxy_cache wwwcache;
#proxy_cache_valid 200 15m;
proxy_pass http://123.30.137.66:8081;
include proxy.inc;
}
location ~ /\.ht {
deny all;
}
}
== the case test..
If Apache httpd service stops: #service httpd stop
Then open in browser this link:
http://www.xaluan.com/modules.php?name=News&file=article&sid=123456
You will see the 502 error with the same URL on browser address.
== Custom error page
I need the config which will help when Apache fail, will show the custom message telling user to wait for 1 minute for service back, then refresh current page with same URL (refresh I can do easy by JavaScript), Nginx does not change URL so JavaScript can work out.
I found an answer that works for me.
In the vhost config file, I put right at the end of the server block, before closing brace:
error_page 502 /502.html;
location = /502.html {
root /home/xaluano/public_html;
}
Of course I also need to create a file 502.html at my domain root, with the meta-tag refresh, and java-script auto refresh.
The content of html page is:
<head>
<meta http-equiv="refresh" content="40" />
</head>
<body>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
var TimerVal = 40;
var TimerSPan = document.getElementById("CDTimer");
function CountDown(){
setTimeout( "CountDown()", 1000 );
TimerSPan.innerHTML=TimerVal;
TimerVal=TimerVal-1;
if (TimerVal<0) { TimerVal=0;
location.reload(true);
// window.location.href = "http://www.xaluan.com";
} //improvement by vivalibre, tq
}
CountDown();
/*]]>*/ </script>
</body>
http://nginx.org/r/error_page
Note that error_page 502 /502.html; performs internal redirect. It does not change the URL in browser address bar.

Resources