What to do with WordPress permalink on nginx? - wordpress

I started a VPS yesterday and I installed WordPress and have done basic setup. But what is the deal with permalink? No one wish to use default ugly URL structure. I tried to understand http://wiki.nginx.org/WordPress but funny thing is "I don't know where to insert those codes". They probably think all of their readers are well known with nginx, that's not funny.
So, I am pretty sure .htaccess is not working in nginx. Now what? Where to place which code to make Custom permalink work? P.S: You know, if I set a custom permalink then the output is 404 page like:
So please, I want a simple guide that tell like "edit demo.php file and place below code after X".
ADDITIONAL INFO: My webroot is /usr/share/nginx/html, and I have two WordPress there. First: /usr/share/nginx/html directory
and second: /usr/share/nginx/html/video directory. I want pretty url for both of them. I chmod wp-content to 775 for both.

Nginx doesn't support .htaccess at all.
Is your nginx setup correctly so that php files are being processed by php-fpm? Your error screen shot is unclear about that.
You have to edit your nginx config file and tell it to use the wordpress controller to direct requests through.
Your nginx.conf should have an http block, and within that a server block, and within that some location blocks. You need to find the correct server block and insert the following location block
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php?$args;
}
Here is a complete example of a fully working wordpress nginx.conf I run:
user www-data;
worker_processes 1;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http {
client_max_body_size 20M;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
sendfile on;
keepalive_timeout 30;
types_hash_max_size 2048;
server_tokens off;
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";
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;
server {
listen 80 default_server;
server_name www.site.com;
root /var/www;
index index.php;
location / {
# Check if a file or directory index file exists, else route it to index.php.
try_files $uri $uri/ /index.php?$args;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}

Related

Nginx configuration is giving me 3 copy's of my domain in the address bar, how is this?

Overview
Okay, before I start, let me say I don't know much of Nginx and has routing works. What I know I learned in about 1 week's time. I'm more of an apache type guy. However, I'm working on a large scale project and would prefer using a faster server and not just apache server. So I decided on Nginx.
This issue relates to CSS/JS files not being resolved within the browser during page rendering for the frontend user.
I've spent over 3 days messing around and keep running into issues. I wouldn't be shocked if it's an easy fix though, so if you know Nginx and want to help me solve this issue please help me. I would, greatly.... greatly, appreciate a helping hand with this.
So I reckon that any dev's that want to help will need a copy of my Nginx config. It's probably also worth mentioning that I am using a hosting panel, called aaPanel on my Ubuntu 20.04 server, hosted in the cloud with IBM.
Other things to note:
aaPanel has two Nginx config files (I'm aware of), I'm messed around on each sub level Nginx config file. If I'm understanding this correctly, aaPanel uses a master Nginx config file and then a copy of the master conf file for your website. I fiddled with both, reverting changes, reloading Nginx etc, etc.
The frontend errors I have received via Opera Console or any other Browser's Console:
Failed to load resource: net::ERR_HTTP2_PROTOCOL_ERROR
Failed to load resource: net::ERR_CONNECTION_CLOSED
The main issue and some thinks I've identified
CSS/JS/IMG/Any MIME TYPE files do not load/resolve.
Any kind of mime type does NOT load, even if I directly visit them in the browser.
Nginx (I am 90% sure it is Nginx) is appending duplicates of my domain name for these files in the path. Seen below (Note that this is not the complete URL, but a small fraction of it since it's over 1mb long. Which leads me to believe it's a stackoverflow/endless loop issue.
https:// domain .net /domain.net/domain.net/domain.net/domain.net/filetype.exstension
(JS/IMG/CSS)
My two Nginx conf file configs I am aware of within aaPanel
My main website's NGINX CONFIG FILE:
server
{
listen 80;
listen 443 ssl http2;
server_name solidbets.net;
index index.php;
root /www/wwwroot/solidbets.net;
#SSL-START SSL related configuration, do NOT delete or modify the next line of commented-out 404 rules
#error_page 404/404.html;
ssl_certificate /www/server/panel/vhost/cert/domain.net/fullchain.pem;
ssl_certificate_key /www/server/panel/vhost/cert/domain.net/privkey.pem;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
#Below ssl_ciphers changed for security PURPOSES
ssl_ciphers EECDH+CCHA20:EEH+CHACHA20-draft:EEH+AES128:RSA+AES128:EDH+A56:RSA+A6:+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;
#SSL-END
#ERROR-PAGE-START Error page configuration, allowed to be commented, deleted or modified
#error_page 404 /404.html;
#error_page 502 /502.html;
#ERROR-PAGE-END
#PHP-INFO-START PHP reference configuration, allowed to be commented, deleted or modified
include enable-php-74.conf;
#PHP-INFO-END
#REWRITE-START URL rewrite rule reference, any modification will invalidate the rewrite rules set by the panel
include /www/server/panel/vhost/rewrite/domain.net.conf;
#REWRITE-END
# Forbidden files or directories
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
# Directory verification related settings for one-click application for SSL certificate
location ./* {
include /www/server/nginx/conf/mime.types;
}
access_log /www/wwwlogs/domain.net.log;
error_log /www/wwwlogs/domain.net.error.log;
}
My MASTER NGINX CONF File for the server:
user www www;
worker_processes auto;
error_log /www/wwwlogs/nginx_error.log crit;
pid /www/server/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
stream {
log_format tcp_format '$time_local|$remote_addr|$protocol|$status|$bytes_sent|$bytes_received|$session_time|$upstream_addr|$upstream_bytes_sent|$upstream_bytes_received|$upstream_connect_time';
access_log /www/wwwlogs/tcp-access.log tcp_format;
error_log /www/wwwlogs/tcp-error.log;
include /www/server/panel/vhost/nginx/tcp/*.conf;
}
events
{
use epoll;
worker_connections 51200;
multi_accept on;
}
http
{
#AAPANEL_FASTCGI_CONF_BEGIN
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_path /dev/shm/nginx-cache/wp levels=1:2 keys_zone=WORDPRESS:100m inactive=60m max_size=1g;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
#AAPANEL_FASTCGI_CONF_END
include mime.types;
#include luawaf.conf;
include proxy.conf;
default_type application/octet-stream;
server_names_hash_bucket_size 512;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server_tokens off;
access_log off;
server
{
listen 888;
server_name phpmyadmin;
index index.html index.htm index.php;
root /www/server/phpmyadmin;
location ~ /tmp/ {
return 403;
}
#error_page 404 /404.html;
include enable-php.conf;
location ~ /\.
{
deny all;
}
access_log /www/wwwlogs/access.log;
}
include /www/server/panel/vhost/nginx/*.conf;
}
URL REWRITE OPTIONS
If anyone has a solution to this very frustrating problem, which may be obvious, please help.

How to configure the sites-available file for a specific site in nginx for a Laravel application?

I'm trying to deploy a Laravel 5.4 application through Nginx. So, I'm more or less following this tutorial.
My server is an Amazon EC2 running Ubuntu 16.04 with PHP 7.0. Nginx version is the 1.10.3.
At the current moment, I do not have a domain to my application, so I can only access it through the ip. I want to access my application through an URL similar to the following:
http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site
However, trying to do it shows an error 403.
If I try to access directly the public folder (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site/public), I'm receiving the home page of my application, but the links are broken. Since I got a Laravel error when I did a mistake, it seems to be working.
The port 80 is open (by this answer), and if I simply create a folder inside /var/www/html and put an index.php file I can access it through the browser. Trying to access http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com shows the Welcome to nginx default page, so nginx is working.
Artisan (php artisan serve) it seems to work in the terminal, but when I try to access it through the browser (http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com:8000), connection is refused (and I opened the port 8000 too).
Trying to access directly through the ip () results in the same behavior for every URL (xxx.xxx.xxx.xxx shows nginx welcome, xxx.xxx.xxx.xxx/my-site returns a 403 error etc.)
I think my problem is with the sites-available files. I'm not sure how to properly name the file for my specific application, and I feel this is the problem - so nginx is not able to identify the file and so apply the configurations in the site:
Without the comments, here is my /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name xxx.xxx.xxx.xxx;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And this is the content of the /etc/nginx/sites-available/my-site file:
server {
listen 80;
listen [::]:80;
root /var/www/html/my-site/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name xxx.xxx.xxx.xxx/my-site;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And here is my nginx.conf file content:
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/*;
}
P.s.: I already created the symbolic link for the sites-enabled folder.
I tried to use the EC2 domain (ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com in the server_name of both files, but it returned an error when I tried to restart nginx.
Removing the /my-site part of the my-site file server_name still returned a 403 error in the ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com/my-site URL. However, if I remove the default nginx welcome file (index.nginx-debian.html), I am able to access the intended home page by the http://ec2-xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com URL, without the my-site in it. Links are working as well; however, JS files both external and inside the same ip are not being loaded due to "Content Security Policy", and the URL is not the one I intended.
So, is there something wrong about these configurations, specifically about the server names and the name of the file?

Nginx with php7.1-fpm showing blank php pages

Background & Environment
I am attempting to deploy a Nginx server with PHP7.1-FPM, following the instructions here and here:
The server is Ubuntu 16.04
Nginx version is 1.13.3
PHP version is
PHP 7.1.9-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Sep 2 2017 05:56:43) ( NTS )
I am fairly new to all this, so compared to a seasoned linux admin, I am still somewhat ignorant of what's what, but I know enough to handle the basics.
The Issue
When I browse to a URL with as inspired01.DOMAIN.com/application/ I get a blank page. There is no entry in the nginx error log.
When I browse to a URL such as inspired01.DOMAIN.com/application/index.php I get a 404 error.
Tried so far
I have attempted the suggestions in the stackoverflow post here. As far as I could tell, my configuration already has the suggested entries that people are saying fixed this problem for them.
I was not able to resolve the issue with those answers. As that post is a few years old, I am thinking perhaps requirements have changed since then.
My Nginx configuration
My nginx.conf is:
user inspired786;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 25;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 64m;
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";
gzip_proxied any;
gzip_comp_level 2;
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/*;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 444;
}
}
The site's conf file is:
server {
listen 80;
listen [::]:80;
server_name inspired01.DOMAIN.com;
access_log /home/inspired786/DOMAIN.com/logs/access.log;
error_log /home/inspired786/DOMAIN.com/logs/error.log;
root /home/inspired786/DOMAIN.com/public/;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Logs
No entry is generated in the Nginx server or site log.
No relevant entry is recorded to the FPM log at /var/log/php7.1-fpm.log
Question
I would like to know if there is anything obvious missing or wrong in this configuration, which would potentially result in php files not being parsed.
UPDATE
As per comment below, pasting here so anyone new sees it:
I enabled debug_connection and went through the masses of log output
that produced. As far as I could tell, there were no issues being
reported. Well, no obvious errors or warnings. So I created a
basic hello.php file including a basic echo command, and also
phpinfo. The file worked fine. So I suspect the issue is something
else. Continuing to investigate.
Consider this question on hold for now, whilst I look into some other possibilities for the blank pages issue.
I run on Linux 2.6, PHP5 and nginx
my PHP file was shown as text
After many test i change the default file in
C:\username\etc\nginx\sites-enabled
as follow:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
#fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
After restart of nginx and PHP services PHP started working!
Check also if yours php files are under root path set in the default file
and begin tests with a test.php file as follow:
<?php
/* test.php */
phpinfo();
?>
Try to fix permissions in your project directory. In Linux this worked for me:
sudo chown -R www-data:www-data .

Why am I not able to have multiple domain names together with nginx?

I have to sub domains I want to redirect to one nginx server: first.domainOne.com and second.domainTwo.net
I have two files in my nginx sites-available directory (each file has a symlink that points to it in sites-enable):
first.server file content:
server {
root /usr/share/nginx/www;
index index.html index.htm;
server_name first.domainOne.com;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
second.server file content:
server{
server_name second.domainTwo.net;
root /usr/share/nginx/test;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
}
When I enable both files, I am only able to access to the content of /usr/share/nginx/www (if I go to first.domainOne.com or second.domainTwo.net).
The only way I have to be able to display my second server's content (/usr/share/nginx/test) is to disable (remove) the first.server file from sites-enable.
Here is my nginx.conf :
user www-data;
worker_processes 4;
pid /var/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;
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/*;
}
EDIT: Both redirections points directly to the server name (ie http://server.name) but the debug log shows it detects the two different URL used):
http://first.domainOne.com -> http://server.name
http://second.domainTwo.net -> http://server.name
server.name is the server on which my nginx instance is hosted.
What do I miss ? Are my configuration files incorrect or do I need to activate an option in nginx.conf ?
Nginx supports multiple server blocks, and your syntax looks reasonable. I suspect the problem may be with something that is not seen above. Try to keep slimming down search server block and see if you can find the problem that way. You can also try removing other files from /etc/nginx/conf.d/*.conf to continue the simplify the problem.
Following General debugging tips for Nginx may also solve your problem.
You might also try starting from simple Nginx server block examples and then building them to match your needs.
Finally, your prose mentioned that there was redirection involved, but I don't see redirection in the code your pasting. Is part of your configuration missing from what you posted? The problem could be in there.

Nginx trys to download file instead of displaying

I install Nginx and have a subdomain and domain. The subdomain has php5-fpm and wordpress. It works fine and is in one sites-available file symlinked to sites-enabled. The domain doesn't have php and has a file also symlinked. Even after restarting the server when I go to the domain it tries to download the html file. Here is my sites-available page for the domain:
server {
server_name www.example.us;
rewrite ^(.*) http://example.us$1 permanent;
}
server {
listen 80;
server_name example.us;
root /var/www/example;
index index.php;
autoindex on;
autoindex_exact_size off;
include /etc/nginx/security;
# Logging --
access_log /var/log/nginx/example.us.access.log;
error_log /var/log/nginx/example.us.error.log notice;
# serve static files directly
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
access_log off;
expires max;
}
# location ~ \.php$ {
# try_files $uri =404;
# fastcgi_pass unix:/var/run/php5-fpm/example.us.socket;
# fastcgi_index index.php;
# include /etc/nginx/fastcgi_params;
# }
}
The nginx.conf file is:
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
log_format gzip '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/log/nginx/access.log gzip buffer=32k;
error_log /var/log/nginx/error.log notice;
# 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;
# Virtual Host Configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Remove default_type application/octet-stream;. This lines makes the browser think it's some binary data and not HTML.
As others noted, this is the line that causes your trouble:
default_type application/octet-stream;
This line overrides the line before it and tells Nginx to send any response as generic binary data. And, since browsers cannot do anything specific with binary data, they just download it as a file.
Removing or commenting that line, as well as the line above:
#include /etc/nginx/mime.types;
#default_type application/octet-stream;
should work, as default Nginx behaviour is to send everything as text/plain and browsers are smart enough to handle HTML/CSS/JS this way.
Despite that I wouldn't recommend such solution, as you may need to serve different types of content from your server, i.e. pictures, mp3's, binaries, etc.
Here's a better solution:
First, check that file /etc/nginx/mime.types; actually exists and accessible to read for the user that runs nginx.
Then, check that the file actually contains types { ... } declarations. That file comes packaged with Nginx and contains sensible defaults for mime-types to filename extensions mapping. If something is wrong with the file you can copy-paste its content from this gist:
https://gist.github.com/kondratovbr/dd34feba1d63bd468ded4ee70e59ea07
And finally, in nginx.conf change
default_type application/octet-stream;
to
default_type text/html;
Now Nginx will serve each type of content based on filename extensions. And if none provided, which is the case with some backend systems/apps, - will try to serve HTML.
Notice that you may want to change some of that defaults. For example, with that setting:
types {
audio/mpeg mp3;
}
browsers will usually try to playback the song rather than straight up download it. Which can be changed, if needed, by declaring:
types {
application/octet-stream mp3;
}
Also try clearing the cache for that site. I was having this problem in Firefox but rolling back to older configs wasn't working

Resources