How can I take off www(sub-domain) if url contains it? - nginx

I've just switched from Apache to Nginx.
Then I'm stucked with this problem.
When I was using Apache, first www(sub-domain) could be removed automatically by having this .htaccess
If I want the same on Nginx, how can I do that?
Here's my old .htaccess
.htaccess (I was using this when I was using Apache)
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(test-sample-site\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Then here's my current conf for Nginx
etc/nginx/conf.d/rails.conf
upstream sample {
ip_hash;
server unix:/var/run/unicorn/unicorn_sample.sock fail_timeout=0;
}
server {
listen 80;
server_name sample.jp;
root /var/www/html/sample/public;
location /wiki {
alias /usr/share/wiki;
index index.php;
}
location ~ ^/wiki.+\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^/wiki(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/wiki$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://sample;
break;
}
}
location ~ ^/assets|system/ {
expires 1y;
add_header Cache-Control public;
log_not_found off;
}
}
UPDATE
upstream sample {
ip_hash;
server unix:/var/run/unicorn/unicorn_sample.sock fail_timeout=0;
}
server {
listen 80;
server_name sample.jp;
root /var/www/html/sample/public;
location /wiki {
alias /usr/share/wiki;
index index.php;
}
location ~ ^/wiki.+\.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^/wiki(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /usr/share/wiki$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://sample;
break;
}
}
location ~ ^/assets|system/ {
expires 1y;
add_header Cache-Control public;
log_not_found off;
}
}
server { # <== redirection server block
server_name www.sample.jp;
return 301 sample.jp$request_uri;
}

Add this server block to your config ( at the bottom or top, but outside the other server block )
server {
server_name www.sample.jp;
return 301 sample.jp$request_uri;
}
Your can add it to your config like this
upstream sample { # <== upstream block
# ...
}
server { # <== server block
# ....
}
server { # <== redirection server block
server_name www.sample.jp;
return 301 sample.jp$request_uri;
}
As for what it would do, the $request_uri will append every thing that was in the original URL to the new one, for example
http://www.example.com/a/b/c
$request_uri = /a/b/c
new URL will be
http://example.com/a/b/c
I believe it should append the query string too, the ?x=y part in http://example.com/a/b?x=y

Related

Nginx throwing 404 for request, instead of performing try_files

I have the following block in my nginx configuration file:
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
When I try any of the following URLs, this works as expected:
http://example.com/cat/test
http://example.com/cat/test/test
http://example.com/cat/t
The following, however, do not. Instead of a redirect, I get a 404 error.
http://example.com/cat/test/
http://example.com/cat/test/test/
http://example.com/cat/t/
I'm not very experienced with nginx, so let me know what further information might be needed. This has me quite stumped.
Edit: I did come across this, but I would prefer to avoid using rewrite (for the sake of efficiency), plus this seems like an unnecessary band-aid:
https://serverfault.com/questions/755646/nginx-use-try-files-to-serve-file-instead-of-directory
Edit 2: Here is the full config
server {
listen 64.40.99.86:80;
server_name example.com;
server_name www.example.com;
client_max_body_size 128m;
root "/var/www/vhosts/example.com/httpdocs";
access_log "/var/www/vhosts/system/example.com/logs/proxy_access_log";
error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";
if ($host ~* ^example.com$) {
rewrite ^(.*)$ http://www.example.com$1 permanent;
}
location / {
proxy_pass http://64.40.99.86:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location /internal-nginx-static-location/ {
alias /var/www/vhosts/example.com/httpdocs/;
add_header X-Powered-By PleskLin;
internal;
}
location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
alias /var/www/vhosts/example.com/web_users/$1/$2;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ ^/~(.+?)(/.*)?$ {
proxy_pass http://64.40.99.86:7080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Accel-Internal /internal-nginx-static-location;
access_log off;
}
location ~ \.php(/.*)?$ {
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
include /etc/nginx/fastcgi.conf;
}
location ~ /$ {
index index.html index.cgi index.pl index.php index.xhtml index.htm index.shtml;
}
location /cat {
try_files $uri $uri/ /index.php?url=$uri;
}
}

Connections between Domain, Nginx and Wordpress

I want to understand the connections between Nginx Config Files, Domains and Wordpress Site URL.
I map my domain blog.example.com to my ip: xxx.xxx.xxx.xxx/wordpress/
in the Wordpress SiteUrl i set blog.example.com
How should my NGINX configuration file look like?
Edit
My Configuration works in the first step, I get to the Frontpage, but when clicking on permalinks, I get redirected to the Frontpage again instead of the Post.
My current Config looks like this:
server {
listen 80 default_server;
root /var/www/wordpress/;
index index.html index.htm index.php
server_name blog.example.com;
location ~\.php$ {
try_files $uri =404;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REQUEST_URI $uri?$args;
}
location / {
try_files $uri /index.php$args;
}
}
Edit
In Nginx Debug Log I see the REQUEST_URI is /wordpress/postname/
but as i understand it should be just /postname/
How about this:
server {
listen 80;
server_name _;
return 404; # default
}
server {
listen 80;
server_name blog.example.com;
location / {
return 301 http://blog.example.com/wordpress;
}
location ^~ /wordpress {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://xxx.xxx.xxx.xxx;
}
}

Nginx Default: Why is location / not forwarded to reverse proxy?

I want NGINX to forward any requests to the domain name http://home.com to proxy_pass http://localhost:8866;. I have set a root root /home/owncloud;, in the NGINX configuration file where the document root of ownCloud is located. This should point to http://home.com/owncloud Under the http://localhost:8866 I have a Docker container running hosting Wordpress. However, NGINX always points the Reverse Proxy to the actual document root of the server (root /home/owncloud) when I try to access http://home.com
Thank you for your input on this - I already browsed through a lot of documentation, but currently I am not able to find a solution myself. Thanks!
This is my NGINX configuration file:
upstream php-handler {
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name home.com;
index index.html index.htm index.php;
# enforce https
return 301 https://$server_name$request_uri;
}
server {
ssl on;
listen 443 ssl;
server_name home.com;
server_name 123.456.789.10 ssl;
ssl_certificate /home/ssl/certificate.pem;
ssl_certificate_key /home/ssl/owncloud.key;
index index.html index.htm index.php;
root /space/owncloud;
try_files $uri $uri/ /index.php?q=$request_uri;
# Add headers to serve security related headers
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Robots-Tag none;
# set max upload size
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
# Disable gzip to avoid the removal of the ETag header
gzip off;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
index index.php index.html index.htm;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location = / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8866;
proxy_read_timeout 90;
}
location ~ ^/(?:\.htaccess|data|config|db_structure\.xml|README){
deny all;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi_params;
proxy_pass_header Authorization;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
}
location /owncloud {
index index.html index.htm index.php;
rewrite ^/caldav(.*)$ /remote.php/caldav$1 redirect;
rewrite ^/carddav(.*)$ /remote.php/carddav$1 redirect;
rewrite ^/webdav(.*)$ /remote.php/webdav$1 redirect;
error_page 403 /core/templates/403.php;
error_page 404 /core/templates/404.php;
location ~ \.php(?:$|/) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param HTTPS on;
fastcgi_param modHeadersAvailable true; #Avoid sending the security headers twice
fastcgi_pass php-handler;
fastcgi_intercept_errors on;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
# attachments can be huge
client_max_body_size 513M;
client_body_in_file_only clean;
# this is where requests body are saved
client_body_temp_path /opt/nginx/bugzilla/data/request_body 1 2;
}
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
If you need to forward all requests another port at your localhost, you need merely this:
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:8866$request_uri;
proxy_read_timeout 90;
}
Note, there is no "=" equal sign before the slash /. location / {} means "exact match of /".
In your code nothing really tryes to forward all requests to localhost. Only requests to index page (document root, i.e. "/") are forwarded.
If you pass all requests to the the process at localhost:8866, none of other location sections will even be tested, so once you make sure your proxy_pass works, you may delete them.

ngnix https redirect with querystring

I have installed ssl on my server and it's working fine but the issue is that if any user try to access site without https than he/she get redirect to https without query string.
http://example.com?av=23423423 to https://example.com only it's not redirect with query string. I have try to add below code but it's not working.
if ($http_x_forwarded_proto = "http") {
return 301 https://$server_name$request_uri;
}
My conf file is below, anybody can help in this?
upstream mysitecombackend {
server unix:/var/run/php-fcgi-mysitecom.sock;
}
upstream exmplecombackend {
server unix:/var/run/php-fcgi-exmplecom.sock;
}
server {
listen 1.2.4.3:443 ssl;
server_name exmple.com *.exmple.com;
ssl on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;
ssl_prefer_server_ciphers on;
ssl_certificate /etc/ssl/www.exmple.com.cabundle;
ssl_certificate_key /etc/ssl/exmple.key;
location / {
proxy_pass http://127.0.0.1:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-Port 443;
# proxy_set_header Host $host$request_uri;
proxy_set_header Host $host;
proxy_set_header Ssl-Offloaded "1";
}
#rewrite ^/(.*) https://exmple.com/$1 permanent;
}
server {
listen 80 default_server;
server_name exmple.com www.exmple.com;
# return 301 https://$host$request_uri;
root /var/www/vhosts/exmple.com/public;
location / {
index index.html index.php;
try_files $uri $uri/ #handler;
expires 30d;
}
location /app/ { deny all; }
location /includes/ { deny all; }
location /lib/ { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/ { deny all; }
location /report/config.xml { deny all; }
location /var/ { deny all; }
location /. {
return 404;
}
location #handler {
rewrite / /index.php;
}
location ~ .php/ {
rewrite ^(.*.php)/ $1 last;
}
include "ssl_offloading.inc";
location ~ .php$ {
if (!-e $request_filename) { rewrite / /index.php last; }
expires off;
fastcgi_pass exmplecombackend;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_param MAGE_RUN_CODE default;
# fastcgi_param MAGE_RUN_TYPE store;
include fastcgi_params;
}
}
You never set $http_x_forwarded_proto to http so the if condition is always false.
You might try comparing the variable to https instead, for example:
if ($http_x_forwarded_proto != "https") {
return 301 https://$server_name$request_uri;
}

Can multi laravel based sites be accessed by folder not by port?

Now, I have two projects(sites) based on Laravel + PHP + MySQL + Nginx, vistors can access them by typing:
http://www.mysite.com:80
http://www.mysite.com:8001
Can I change the accessing method to virtual folder not by port?
http://www.mysite.com/project1
http://www.mysite.com/project2
The nginx conf files are (at /etc/nginx/conf.d/):
project1.conf
server {
listen *:80;
server_name mysite.com www.mysite.com;
server_tokens off;
root /var/www/html/project1/public;
client_max_body_size 100m;
access_log /var/log/nginx/project1_access.log;
error_log /var/log/nginx/project1_error.log;
location / {
index index.php index.html;
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
project2.conf
server {
listen *:80;
server_name www.mysite.com;
server_tokens off;
root /var/www/html/project2/public;
client_max_body_size 100m;
access_log /var/log/nginx/project2_access.log;
error_log /var/log/nginx/project2_error.log;
location / {
index index.php index.html;
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
The only decent way to do it is by folder and virtual host rather than port.
Sure - an example config will look like this:
1 Define your app servers
server {
listen 8080;
root /var/www/html/project1/public;
......
}
server {
listen 8081;
root /var/www/html/project2/public;
......
}
2 Define your proxy server
server {
listen 80;
server_name mysite.com www.mysite.com;
.....
location /project1 {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
.....
}
location /project2 {
proxy_pass http://localhost:8081;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
....
}
}

Resources