I have a site that uses subdirectories and currently only works when the trailing slash is added to the URL ("http://www.domain.com/dir/"). When there is no trailing slash, I get "unable to connect at server domain.com:8080" (8080 is the listening port Nginx is set up for).
I've tried adding the rewrite suggested here (and here), but it results in the "cannot connect" error for the entire virtual host.
Is there another way to add the trailing slash that I could try? Or, is there a way I can configure it to see the URL as a directory (and thus, look for the index file), regardless of the presence of the trailing slash?
Edit
Nginx.conf:
user www-data;
worker_processes 4;
error_log /var/log/nginx/error.log;
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 on;
tcp_nodelay on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
map $scheme $fastcgi_https { ## Detect when HTTPS is used
default off;
https on;
}
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Server block:
server {
listen 8080;
server_name domain.com www.domain.com;
include www.inc;
root /var/vhosts/domain/current/frontend/;
include php.inc;
}
Php.inc:
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param ENVIRONMENT production;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_intercept_errors on;
fastcgi_connect_timeout 10;
fastcgi_send_timeout 15;
fastcgi_read_timeout 120;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
www.inc:
if ($host ~ ^([^\.]+\.com)) {
rewrite ^/(.*)$ http://www.$host/$1 permanent;
}
Change server {} block to
server {
listen 8080;
port_in_redirect off;
server_name www.domain.com domain.com; #Order matters!
include www.inc;
root /var/vhosts/domain/current/frontend/;
include php.inc;
}
Related
I changed the following lines in my nginx conf
listen 80 default_server;
server_name _;
To
listen 80 default_server;
server_name mydomain.com;
return 301 https://server_name$request_uri;
I've removed the redirection to https change that I made however nginx is still redirecting my requests to https which is not supported currently. How do I change it back?
The rest of my nginx conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
server {
listen 80 default_server;
server_name _;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_buffer_size 8192;
uwsgi_buffering on;
}
location ~ [^/]\.php(/|$) {
root /opt;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
fastcgi_param HTTP_PROXY "";
fastcgi_read_timeout 150;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
#include /etc/nginx/conf.d/*.conf;
}
}
Are there any other settings that need to be checked?
It was a browser cache issue because of using the 301.
Opened the urls in incognito and they worked fine.
I have Nginx with three domains.
The sites, alphabetically are d.com, g.com, and m.com.
All three sites are single site WordPress installations.
g.com is https with a letsencrypt certificate and loads as expected.
m.com is not https and loads as expected
d.com loads m.com instead of its own folder.
I've been searching and reading for two weeks now and I cannot sort out what I have done wrong.
NEW INFORMATION
I discovered that the DNS for this was still at my old host ( mt ), it was pointing here correctly, but I decided to move it to the new host ( linode ).
Now if I use www.d.com I get the correct site loading from the correct folder. But if I leave off the www and just use d.com, I get redirected to www.m.com as a full redirect.
/etc/nginx/nginx.conf
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;
client_max_body_size 150M;
# server_tokens off;
server_names_hash_bucket_size 64;
# server_name_in_redirect off;
#fastcgi
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
# /snippits/ssl-params.conf
##
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
#ssl_prefer_server_ciphers on;
##
# Logging Settings
# Logs set in server blocks
##
error_log /var/log/nginx/http_error.log error;
##
# Gzip Settings
# /conf.d/gzip.conf
##
##
# 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/d.com
server {
listen 80;
listen [::]:80;
server_name d.com www.d.com;
include snippets/expires-headers.conf;
root /var/www/html/d.com/public_html;
index index.php;
access_log /var/log/nginx/d.com/www-access.log;
error_log /var/log/nginx/d.com/www-error.log error;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/d.com/public_html$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
/etc/nginx/sites-available/g.com
server {
listen 80;
listen [::]:80;
server_name g.com www.g.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /var/lib/acme/live/g.com/fullchain;
ssl_certificate_key /var/lib/acme/live/g.com/privkey;
include snippets/ssl-params.conf;
include snippets/expires-headers.conf;
server_name g.com www.g.com;
root /var/www/html/g.com/public_html;
index index.html index.php;
access_log /var/log/nginx/g.com/www-access.log;
error_log /var/log/nginx/g.com/www-error.log error;
location /.well-known/acme-challenge/ {
alias /var/run/acme/acme-challenge/;
}
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/g.com/public_html$fastcgi_script_name;
}
}
/etc/nginx/sites-available/m.com
server {
listen 80;
listen [::]:80;
server_name m.com www.m.com;
include snippets/expires-headers.conf;
root /var/www/html/m.com/public_html;
index index.php;
access_log /var/log/nginx/m.com/www-access.log;
error_log /var/log/nginx/m.com/www-error.log error;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/m.com/public_html$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
There seems to be nothing wrong with your config, but my suspicion is that you created the config for d.com by copy/pasting the config from m.com and saved the file before making any changes and then either NGINX restarted or reloaded its configuration before you made your modifications to d.com config, which basically means that NGINX doesn't have your current configuration loaded.
You could reload the configuration. On Ubuntu/Debian is something like this:
sudo service nginx reload
You should also recheck and be absolutely sure that /var/www/html/d.com/public_html and /var/www/html/m.com/public_html are indeed serving different content.
After moving the DNS Zone file from Media Temple to Linode, http://www.d.com started loading the WordPress install from the correct folder, though http://d.com did not.
I tried another browser and found that in the other browser both were now working.
I don't understand why moving the DNS Zone file worked here.
I have a medium to large size WordPress site running off a MediaTemple NGINX server, and CENTOS and I'm having trouble getting any location block properties applied. What the goal is, is to have a directory locked down so that only the server has access to it. From everything I've seen, the root I'm setting and the locations blocks are being called correctly, they just don't seem to be being noticed.
#user nginx;
worker_processes 24;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# rewrite_log on;
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 3;
#tcp_nodelay on;
#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)”;
index index.php index.html index.htm
server_tokens off;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
include /etc/nginx/conf.d/*.conf;
include fastcgi.conf;
server {
listen 80;
server_name domainname.com;
rewrite ^ $scheme://www.domainname.com$request_uri redirect;
}
server {
listen 80;
server_name www.domainname.com;
root /var/www/vhosts/domainname.com/httpdocs;
# Additional rules go here.
location ^~ /protected-folder/ {
allow 127.0.0.1;
deny all;
}
# include global/restrictions.conf;
location ~* \.php$ {
try_files $uri =404; # This is not needed if you have cgi.fix_pathinfo = 0 in php.ini (you should!)
fastcgi_pass 127.0.0.1:9000;
}
# Only include one of the files below.
include global/wordpress.conf;
# include global/wordpress-ms-subdir.conf;
# include global/wordpress-ms-subdomain.conf;
}
}
After every change I'm running:
sudo service nginx restart
Do I have to do a full server reboot?
Is there something wrong with the syntax above?
For any imports above, the content follows almost identical to the WordPress article on setting up NGINX for WordPress.
Any help on this would be appreciated.
I'm using pretty simple nginx boilerplate here, but it appears my server isn't listening when I attempt to access my domain's root. Here's my nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name blog.dev.company.com ;
root /var/www/wordpress/ ;
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
}
}
From my understanding, I should be getting the index.php I have set up in /var/www/wordpress when I make a request to blog.dev.company.com. Instead, the site just hangs. The domain name is found, but nginx does not process the request. The nginx access log is empty. What am I missing?
Do you have a location such as this to handle PHP scripts?
# Pass the PHP scripts to FastCGI server
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
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.