Nginx overriding the default server - nginx

I am trying to serve a webpy application under Nginx on debian linux (RasPi).
I can successfully serve the webpy app if I use any port other than 80. But If I try to use port 80, it is not possible to hit my webpy app, I will always see instead the default "Welcome to Nginx" page.
I have tried everything I can think of the disable the default page and override it with my webpy app but nothing seems to work. I have deleted the default link, and file out of their respective directories. I have tried pointing directly to sites-enabled/webpy rather than sites-enabled/* in nginx.conf. Still result is always the same, if I hit http://[ip-address]/ I will see the welcome to nginx page.
I have tried several times nginx -s reload and stopping/starting everything. And rebooting the device.
How do I override this so that it is serving my webpy app on port 80?
nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*; # I even tried pointing directly to webpy instead of *
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
As far as I can tell this should make any thing inside of sites-enabled/ get served
I have only 1 thing inside of sites-enabled/
$ ls -la
total 8
drwxr-xr-x 2 root root 4096 May 31 17:49 .
drwxr-xr-x 6 root root 4096 May 31 18:08 ..
lrwxrwxrwx 1 root root 32 May 31 17:49 webpy -> /etc/nginx/sites-available/webpy
which is a link to sites-available/webpy
# webpy server
server{
listen 80; # if I set this to 8080 then I can hit the app on that port.
location / {
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
}
location /static/ {
root /path/to/www;
if (-f $request_filename) {
rewrite ^/static/(.*)$ /static/$1 break;
}
}
}
I don't have any other files, or links inside of sites-enabled/ or sites-avaialbe/ I removed the default ones from these folders.

When you set
listen 8080;
run
sudo netstat -tlnp | grep nginx
and see what ports nginx is listening on. It should not be listening on port 80 at this point. If it is try
sudo grep -rnIw "80" /etc/nginx
If nginx is listening on port 80, there must be a declaration for it somewhere under /etc/nginx
As a sanity check, I would also stop the nginx server completely
sudo service nginx stop
and see if some other server is serving the default nginx page.

Related

Nginx still showing default index on subdomain

I have a problem on my Virtual Private Server : I already configured a "main" website which works fine, but I recently tried to create a sub domain and I am going to the "Welcome to nginx" every time I try to reach it. Here are my configuration files :
sub.domain.com (in /etc/nginx/sites-available)
server {
listen 80;
server_name sub.domain.com www.sub.domain.com;
root /home/user/www/sub;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Nginx.conf (but should be good considering that it works for domain.com)
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
And the file system at /home/user/
---- domain.com ... (more files)
|
user ---
| --- index.php
----sub --- --- addFile.php
--- files (empty)
--- data.xml
--- data.dtd
I don't know a lot about nginx config files I just copied the default one and modified the server name and the path, but this file seems to be okay so I'm still wondering why is this not working. Thank you for your help !
the active nginx configurations are made with symbolic links from /etc/nginx/sites-available/vhost.conf to /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/vhost.conf /etc/nginx/sites-enabled/
in sites-enabled you have a default active, that shows the nginx index (Welcome to nginx).
If you want delete it.
I recomend you to change your php-fpm pool to work for port
vim /etc/php/7.0/fpm/pool.d/www.conf
and change the line:
listen = /run/php/php-fpm.sock for listen = 127.0.0.1:9001 (or other port)
and restart the fpm, /etc/init.d/php-fpm restart
if you do a netstat -plnt it will show you the php-fpm working in port.
root#server# netstat -plnt
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9001 0.0.0.0:* LISTEN 15230/php-fpm.conf)
and change your vhost in this way:
server {
listen 80;
server_name sub.domain.com www.sub.domain.com;
root /home/user/www/sub;
index index.php index.html index.htm;
location / {
#try_files $uri $uri/ =404;
try_files $uri $uri/ /index.php$args; #try with this
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass 127.0.0.1:9003; #your fpm port
fastcgi_index index.php;
fastcgi_buffer_size 128k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
access_log /var/log/nginx/subdomain__access.log;
error_log /var/log/nginx/subdomain_error.log;
}#End server Block
When you have the new active configuration (symbolic link) on /etc/.../sites-enabled/
do
nginx -t
to run a test to see if the nginx configuration is good.
root#server:# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If this is not working, check the log of the application and the nginx error log.
/var/log/nginx/error.log
/var/log/nginx/subdomain_error.log
(When the things are not working is where your sysadmin magic should come out)
Good luck!

Nginx geoip environment variables Undefined

I've just try this documentation : https://www.howtoforge.com/tutorial/how-to-use-geoip-with-nginx-on-ubuntu-16.04/
Here's my environment :
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 16.04.3 LTS
Release: 16.04
Codename: xenial
$ uname -a
Linux diameter 4.11.0-14-generic #20~16.04.1-Ubuntu SMP Wed Aug 9 09:06:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
the nginx.conf :
$ cat /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # the city IP database
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
The vhost conf :
$ cat /etc/nginx/sites-enabled/diameter.vhost
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# 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/david/*****/diameter;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name diameter.********
fr.diameter.*******
uk.diameter.*******
us.diameter.*******;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}
Fastcgi parameters :
$ cat /etc/nginx/fastcgi_params
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
### SET GEOIP Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
And environment variables are undefined. Here's the log :
==> /var/log/nginx/error.log <==
2017/10/16 14:40:37 [error] 3533#3533: *5 FastCGI sent in stderr: "PHP message: PHP Notice: Undefined index: GEOIP_COUNTRY_CODE in /home/david/******/diameter/index.php on line 2" while reading response header from upstream, client: 92.154.72.118, server: diameter.******, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "fr.diameter.**********"
I've certainly miss a point. I do not know which one actually.
thanks for your help,
David.
First thing, you should use IPV6 databases of GeoIP, thay also work for IPV4 addresses.
Secondly, the Nginx GeoIP module is dynamic, so you need to enable it :
sudo ln -sf /usr/share/nginx/modules-available/mod-http-geoip.conf /etc/nginx/modules-enabled/
Last thing, in /etc/nginx/ there are two files, fastcgi_params and fastcgi.conf. If unsure, put the GeoIP variables in both.

PHP7.0-fpm extremly slow on Ubuntu Windows Subsystem Linux

I installed Windows Subsystem Ubuntu shell recently and shifted all of my development from XAMPP to nginx and php7.0-fpm installed through ubuntu windows subsystem.
The problem i am facing is that php files load extremly slower. For a test I simply put
<?php phpinfo(); ?>
in a file and executed it. It literally took the system two minutes to return the reply. I have debugged a lot but could not find any solution.
I am running nginx through nginx server blocks and have setup my local domains.
I am sure that php is slower by observing that if i load a static file i.e a txt or html files, it loads blazingly fast.
Below are my sites enabled files and nginx conf file ..
Sites Enabled
server {
listen 80 ;
listen [::]:80;
root /mnt/c/xampp/htdocs/doit/;
index index.html index.php;
server_name doit.dev www.doit.dev;
error_log /var/log/nginx/error.log;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_read_timeout 120;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
Nginx Conf File:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
Error logs for both nginx and php-fpm working, nothing being logged against errors.
Using windows 10 v1803, and nginx & php7 fpm via WSL.
Changing listen to 127.0.0.1:9000 doesn't work for me.
After spending hours of googling, I found:
https://github.com/Microsoft/WSL/issues/2100
i.e. Add
fastcgi_buffering off;
to nginx.conf, and save my day.
Sorted this out, pasting for any other enthusiast working with ubuntu on windows subsystem.
The default Nginx and php setups are going to use a unix:socket, but that’s not going to work for WSL. Also, WSL uses a lightweight init system and services are not going to start automatically for Nginx, PHP, MySQL, etc.
Edit /etc/nginx/sites-available/example.com.conf
comment out fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
and add fastcgi_pass 127.0.0.1:9000;
Edit /etc/php/7.0/fpm/pool.d/www.conf
comment out listen = /var/run/php/php7.0-fpm.sock;
and add listen = 127.0.0.1:9000;
It will fix all of your issues.

Why is NGINX is ignoring my query strings?

I recently switched from APACHE to NGINX. However, somewhere along the way NGINX started ignoring my query strings. For example, I use pagination like so:
http://example.com/index.php?page=5
This simply loads example.com as if the query string wasn't there.
Here's the configuration (edited as suggested by Nelson):
/etc/nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
/etc/nginx/sites-available/default:
server {
root /usr/share/nginx/www/;
index index.php;
# Make site accessible from http://localhost/
server_name localhost;
# Pass PHP scripts to PHP-FPM
location ~ \.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
client_max_body_size 8M;
}
I was having the same problem. I changed the location section in my nginx virtual host file as below and it works fine for me.
location / {
# try_files $uri $uri/ /index.php;
try_files $uri $uri/ /index.php$is_args$args;
}
Just use this as Location :
location ~ \.php {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
}
To be more specific your following two lines are removing the query string:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(/.+)$;

Nginx port_in_redirect not working?

I recently set up a Nginx server to host a wordpress install and am using Varnish as a reverse proxy in front of the server. Varnish is running on port 80, so I have set up Nginx to listen to 80 and redirect. Unfortunately on redirection, the port 8080 is appended to the nginx request.
Including port_in_redirect off seems to be the general solution to this problem, but it doesn't appear to be working for me. I've attached the /sites-enabled/default config below. Am I doing anything wrong?! The php redirections seems to work fine, it's only at location /where it fails.
/sites-enabled/default:
server {
listen 8080 default;
server_name "" xxx.xxx.xxx.xxx; #just using IP here (no domain yet)
port_in_redirect off;
server_name_in_redirect off;
access_log /var/log/nginx/localhost.access.log;
location / {
root /var/www/site/html/;
index index index.php;
try_files $uri/ $uri /index.php?q=$uri&$args;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/site/html/$fastcgi_script_name;
include fastcgi_params;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_param SERVER_PORT 80;
}
location ~ /.ht {
deny all;
}
location ~ /.git {
deny all;
}
location ~ /.svn {
deny all;
}
}
upstream backend {
server 127.0.0.1:9000;
}
nginx.conf:
user www-data;
worker_processes 1;
error_log /var/log/nginx/error.log debug;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
include /etc/nginx/mime.types;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush off;
keepalive_timeout 30;
tcp_nodelay on;
gzip on;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
nginx: nginx version: nginx/1.0.9
Actually, just sorted it. Simple error - in the Wordpress admin site wp-admin > Settings > WordPress address (URL) and Site address (URL) both had the port 8080 in the URL! Hence the redirection. The above setup works fine like this.
If that doesn't solve your problem a one tip I learned was to add print_r($_SERVER["SERVER_PORT"]); in your index.php to ensure you are getting the correct port (80 in my case) set from fastcgi.
Hopefully this simple step can save someone some time!
Another solution is to have your Nginx server block listen on your localhost IP, either 127.0.0.1 or [::1] (if you have IPv6 -- picks up IPv4, too) on either port 80 or 8080, then have Varnish listen to the outside world on your external IP, xx.xx.xx.xx or [::], on the same port you used for the Nginx server block. Then you don't have to redirect or defer or anything.

Resources